// 웹타입 컴포넌트들을 내보내는 인덱스 파일
import React from "react";
import { WebTypeComponentProps } from "@/lib/registry/types";

// 개별 컴포넌트 import
import { TextWidget } from "./TextWidget";
import { NumberWidget } from "./NumberWidget";
import { DateWidget } from "./DateWidget";
import { SelectWidget } from "./SelectWidget";
import { TextareaWidget } from "./TextareaWidget";
import { CheckboxWidget } from "./CheckboxWidget";
import { RadioWidget } from "./RadioWidget";
import { FileWidget } from "./FileWidget";
import { ImageWidget } from "./ImageWidget";
import { CodeWidget } from "./CodeWidget";
import { EntityWidget } from "./EntityWidget";
import { RatingWidget } from "./RatingWidget";

// 개별 컴포넌트 export
export { TextWidget } from "./TextWidget";
export { NumberWidget } from "./NumberWidget";
export { DateWidget } from "./DateWidget";
export { SelectWidget } from "./SelectWidget";
export { TextareaWidget } from "./TextareaWidget";
export { CheckboxWidget } from "./CheckboxWidget";
export { RadioWidget } from "./RadioWidget";
export { FileWidget } from "./FileWidget";
export { ImageWidget } from "./ImageWidget";
export { CodeWidget } from "./CodeWidget";
export { EntityWidget } from "./EntityWidget";
export { RatingWidget } from "./RatingWidget";

// 컴포넌트 이름으로 직접 매핑하는 함수 (DB의 component_name 필드용)
export const getWidgetComponentByName = (componentName: string): React.ComponentType<WebTypeComponentProps> => {
  switch (componentName) {
    case "TextWidget":
      return TextWidget;
    case "NumberWidget":
      return NumberWidget;
    case "DateWidget":
      return DateWidget;
    case "SelectWidget":
      return SelectWidget;
    case "TextareaWidget":
      return TextareaWidget;
    case "CheckboxWidget":
      return CheckboxWidget;
    case "RadioWidget":
      return RadioWidget;
    case "FileWidget":
      return FileWidget;
    case "ImageWidget":
      return ImageWidget;
    case "CodeWidget":
      return CodeWidget;
    case "EntityWidget":
      return EntityWidget;
    case "RatingWidget":
      return RatingWidget;
    default:
      // console.warn(`알 수 없는 컴포넌트명: ${componentName}, TextWidget으로 폴백`);
      return TextWidget;
  }
};

// 기본 컴포넌트 매핑 룰 (카테고리나 타입 기반)
export const getWidgetComponentByWebType = (webType: string): React.ComponentType<WebTypeComponentProps> => {
  // 기본 매핑 룰 - 웹타입에 따른 컴포넌트 결정
  switch (webType.toLowerCase()) {
    case "text":
    case "email":
    case "tel":
    case "url":
    case "password":
      return TextWidget;

    case "number":
    case "decimal":
    case "integer":
    case "float":
      return NumberWidget;

    case "date":
    case "datetime":
    case "time":
      return DateWidget;

    case "select":
    case "dropdown":
    case "combobox":
      return SelectWidget;

    case "textarea":
    case "text_area":
    case "multiline":
      return TextareaWidget;

    case "checkbox":
    case "boolean":
    case "toggle":
      return CheckboxWidget;

    case "radio":
    case "radiobutton":
      return RadioWidget;

    case "file":
    case "upload":
    case "attachment":
      return FileWidget;

    case "image":
    case "img":
    case "picture":
    case "photo":
      return ImageWidget;

    case "code":
    case "script":
      return CodeWidget;

    case "entity":
    case "reference":
    case "lookup":
      return EntityWidget;

    case "rating":
    case "star":
    case "score":
      return RatingWidget;

    default:
      // 기본적으로 텍스트 위젯 사용
      return TextWidget;
  }
};

// 동적 웹타입 컴포넌트 맵 생성 함수
export const createWebTypeComponents = (
  webTypes: string[],
): Record<string, React.ComponentType<WebTypeComponentProps>> => {
  const components: Record<string, React.ComponentType<WebTypeComponentProps>> = {};

  webTypes.forEach((webType) => {
    components[webType] = getWidgetComponentByWebType(webType);
  });

  return components;
};

// 기존 하드코딩된 맵 (호환성 유지)
export const WebTypeComponents: Record<string, React.ComponentType<WebTypeComponentProps>> = {
  text: TextWidget,
  email: TextWidget,
  tel: TextWidget,
  number: NumberWidget,
  decimal: NumberWidget,
  date: DateWidget,
  datetime: DateWidget,
  select: SelectWidget,
  dropdown: SelectWidget,
  textarea: TextareaWidget,
  text_area: TextareaWidget,
  boolean: CheckboxWidget,
  checkbox: CheckboxWidget,
  radio: RadioWidget,
  file: FileWidget,
  image: ImageWidget,
  code: CodeWidget,
  entity: EntityWidget,
  rating: RatingWidget,
};
