/**
 * 字段渲染策略注册表
 * 工厂模式：统一管理所有字段类型的渲染策略
 */

import type { FieldType } from '../types';
import type { IFieldRenderStrategy } from './IFieldRenderStrategy';
import { InputStrategy } from './strategies/InputStrategy';
import { TextAreaStrategy } from './strategies/TextAreaStrategy';
import { NumberStrategy } from './strategies/NumberStrategy';
import { SelectStrategy } from './strategies/SelectStrategy';
import { RadioStrategy } from './strategies/RadioStrategy';
import { CheckboxStrategy } from './strategies/CheckboxStrategy';
import { DateStrategy } from './strategies/DateStrategy';
import { DateTimeStrategy } from './strategies/DateTimeStrategy';
import { TimeStrategy } from './strategies/TimeStrategy';
import { SwitchStrategy } from './strategies/SwitchStrategy';
import { SliderStrategy } from './strategies/SliderStrategy';
import { RateStrategy } from './strategies/RateStrategy';
import { UploadStrategy } from './strategies/UploadStrategy';
import { CascaderStrategy } from './strategies/CascaderStrategy';

/**
 * 策略注册表
 */
class FieldRenderStrategyRegistry {
  private strategies = new Map<FieldType, IFieldRenderStrategy>();

  constructor() {
    // 注册所有内置策略
    this.register('input', new InputStrategy());
    this.register('textarea', new TextAreaStrategy());
    this.register('number', new NumberStrategy());
    this.register('select', new SelectStrategy());
    this.register('radio', new RadioStrategy());
    this.register('checkbox', new CheckboxStrategy());
    this.register('date', new DateStrategy());
    this.register('datetime', new DateTimeStrategy());
    this.register('time', new TimeStrategy());
    this.register('switch', new SwitchStrategy());
    this.register('slider', new SliderStrategy());
    this.register('rate', new RateStrategy());
    this.register('upload', new UploadStrategy());
    this.register('cascader', new CascaderStrategy());
  }

  /**
   * 注册策略
   */
  register(fieldType: FieldType, strategy: IFieldRenderStrategy): void {
    this.strategies.set(fieldType, strategy);
  }

  /**
   * 获取策略（工厂方法）
   */
  get(fieldType: FieldType): IFieldRenderStrategy | undefined {
    return this.strategies.get(fieldType);
  }

  /**
   * 检查是否已注册
   */
  has(fieldType: FieldType): boolean {
    return this.strategies.has(fieldType);
  }
}

// 单例模式：全局唯一的注册表实例
export const fieldRenderStrategyRegistry = new FieldRenderStrategyRegistry();

