'use client';

import type { FieldConfig } from '@/types/invyone-component';
import { TextField } from './TextField';
import { NumberField } from './NumberField';
import { DateField } from './DateField';
import { DateTimeField } from './DateTimeField';
import { TimeField } from './TimeField';
import { DateRangeField } from './DateRangeField';
import { SelectField } from './SelectField';
import { EntityField } from './EntityField';
import { CheckboxField } from './CheckboxField';
import { TextareaField } from './TextareaField';
import { FileField } from './FileField';
import { CodeField } from './CodeField';

export interface FieldRendererProps {
  field: FieldConfig;
  value: any;
  onChange: (value: any) => void;
  mode: 'form' | 'search';
  disabled?: boolean;
  error?: string;
}

/**
 * FieldConfig.type을 보고 적절한 입력 위젯을 렌더한다.
 *
 * mode='form': 폼 렌더링 규칙 (date→DatePicker, select→Select)
 * mode='search': 검색 렌더링 규칙 (date→DateRangePicker, select→MultiSelect)
 */
export function FieldRenderer(props: FieldRendererProps) {
  switch (props.field.type) {
    case 'text':
      return <TextField {...props} />;
    case 'number':
      return <NumberField {...props} />;
    case 'date':
      return <DateField {...props} />;
    case 'datetime':
      return <DateTimeField {...props} />;
    case 'time':
      return <TimeField {...props} />;
    case 'daterange':
      return <DateRangeField {...props} />;
    case 'select':
      return <SelectField {...props} />;
    case 'entity':
      return <EntityField {...props} />;
    case 'checkbox':
      return <CheckboxField {...props} />;
    case 'textarea':
      return <TextareaField {...props} />;
    case 'file':
      return <FileField {...props} />;
    case 'code':
      return <CodeField {...props} />;
    default:
      return <TextField {...props} />;
  }
}
