"use client";

/**
 * V2TableSearchWidget 설정 패널
 * 기존 TableSearchWidgetConfigPanel의 모든 로직(필터 모드, 대상 패널, 고정 필터 등)을 유지하면서
 * componentConfigChanged 이벤트를 추가하여 실시간 업데이트 지원
 */

import React from "react";
import { TableSearchWidgetConfigPanel } from "@/lib/registry/components/v2-table-search-widget/TableSearchWidgetConfigPanel";

interface V2TableSearchWidgetConfigPanelProps {
  component?: any;
  config?: any;
  onUpdateProperty?: (property: string, value: any) => void;
  onChange?: (newConfig: any) => void;
  tables?: any[];
}

export function V2TableSearchWidgetConfigPanel({
  component,
  config,
  onUpdateProperty,
  onChange,
  tables,
}: V2TableSearchWidgetConfigPanelProps) {
  const handleChange = (newConfig: any) => {
    if (onChange) {
      onChange(newConfig);
    }

    if (typeof window !== "undefined") {
      window.dispatchEvent(
        new CustomEvent("componentConfigChanged", {
          detail: { config: newConfig },
        })
      );
    }
  };

  const handleUpdateProperty = (property: string, value: any) => {
    if (onUpdateProperty) {
      onUpdateProperty(property, value);
    }

    if (typeof window !== "undefined") {
      window.dispatchEvent(
        new CustomEvent("componentConfigChanged", {
          detail: { property, value },
        })
      );
    }
  };

  return (
    <TableSearchWidgetConfigPanel
      component={component}
      config={config}
      onUpdateProperty={onChange ? undefined : handleUpdateProperty}
      onChange={onChange ? handleChange : undefined}
      tables={tables}
    />
  );
}

V2TableSearchWidgetConfigPanel.displayName = "V2TableSearchWidgetConfigPanel";

export default V2TableSearchWidgetConfigPanel;
