/**
 * @fileoverview Configuration system for tab history management with configurable limits and pruning strategies.
 *
 * This module provides:
 * - TabConfig interface for defining history behavior
 * - DEFAULT_TAB_CONFIG with sensible defaults
 * - TAB_CONFIG injection token for dependency injection
 * - Utility functions for config management
 *
 * The configuration controls how tab navigation history is managed, including
 * size limits, pruning strategies, and validation behavior.
 */

import { InjectionToken } from '@angular/core';

/**
 * Configuration interface for tab history management.
 *
 * Controls how navigation history is stored and managed for individual tabs,
 * including automatic pruning when limits are exceeded.
 */
export interface TabConfig {
  /**
   * Maximum number of history entries per tab
   * @default 50
   */
  maxHistorySize: number;

  /**
   * Maximum number of forward history entries to keep when navigating to a new location
   * @default 10
   */
  maxForwardHistory: number;

  /**
   * Strategy for pruning history when size limit is exceeded
   * - 'oldest': Remove oldest entries first
   * - 'balanced': Keep entries around current position
   * - 'smart': Intelligent pruning based on navigation patterns
   * @default 'balanced'
   */
  pruningStrategy: 'oldest' | 'balanced' | 'smart';

  /**
   * Enable automatic index validation after operations
   * @default true
   */
  enableIndexValidation: boolean;

  /**
   * Log warnings when history is pruned
   * @default false
   */
  logPruning: boolean;
}

/**
 * Default configuration for tab history management.
 *
 * Provides balanced settings suitable for most applications:
 * - 50 history entries max (prevents memory bloat)
 * - 10 forward history entries (reasonable redo depth)
 * - Balanced pruning strategy (keeps recent + nearby entries)
 * - Index validation enabled (maintains data integrity)
 * - Pruning logs disabled (reduces console noise)
 */
export const DEFAULT_TAB_CONFIG: TabConfig = {
  maxHistorySize: 50,
  maxForwardHistory: 10,
  pruningStrategy: 'balanced',
  enableIndexValidation: true,
  logPruning: false,
};

/**
 * Angular injection token for tab configuration.
 *
 * Use this token to inject tab configuration into services and components.
 * Defaults to DEFAULT_TAB_CONFIG when not explicitly provided.
 *
 * @example
 * ```typescript
 * constructor(@Inject(TAB_CONFIG) private config: TabConfig) {}
 * ```
 */
export const TAB_CONFIG = new InjectionToken<TabConfig>('TAB_CONFIG', {
  providedIn: 'root',
  factory: () => DEFAULT_TAB_CONFIG,
});

/**
 * Creates a tab configuration by merging partial config with defaults.
 *
 * Useful for creating custom configurations while maintaining defaults
 * for unspecified properties.
 *
 * @param partialConfig - Partial configuration to merge with defaults
 * @returns Complete TabConfig with merged values
 *
 * @example
 * ```typescript
 * const customConfig = createTabConfig({
 *   maxHistorySize: 100,
 *   logPruning: true
 * });
 * // Results in: { maxHistorySize: 100, logPruning: true, ...other defaults }
 * ```
 */
export function createTabConfig(partialConfig?: Partial<TabConfig>): TabConfig {
  return {
    ...DEFAULT_TAB_CONFIG,
    ...partialConfig,
  };
}