"use client";

/**
 * V2AggregationWidget 설정 패널
 * 기존 AggregationWidgetConfigPanel의 모든 로직(테이블/컬럼 Combobox, 집계 항목 관리,
 * 필터 조건, 데이터 소스 선택, 스타일 설정 등)을 유지하면서
 * componentConfigChanged 이벤트를 추가하여 실시간 업데이트 지원
 */

import React from "react";
import { AggregationWidgetConfigPanel } from "@/lib/registry/components/v2-aggregation-widget/AggregationWidgetConfigPanel";
import type { AggregationWidgetConfig } from "@/lib/registry/components/v2-aggregation-widget/types";

interface V2AggregationWidgetConfigPanelProps {
  config: AggregationWidgetConfig;
  onChange: (config: Partial<AggregationWidgetConfig>) => void;
  screenTableName?: string;
  screenComponents?: Array<{
    id: string;
    componentType: string;
    label?: string;
    tableName?: string;
    columnName?: string;
  }>;
}

export const V2AggregationWidgetConfigPanel: React.FC<V2AggregationWidgetConfigPanelProps> = ({
  config,
  onChange,
  screenTableName,
  screenComponents,
}) => {
  const handleChange = (newConfig: Partial<AggregationWidgetConfig>) => {
    onChange(newConfig);

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

  return (
    <AggregationWidgetConfigPanel
      config={config}
      onChange={handleChange}
      screenTableName={screenTableName}
      screenComponents={screenComponents}
    />
  );
};

V2AggregationWidgetConfigPanel.displayName = "V2AggregationWidgetConfigPanel";

export default V2AggregationWidgetConfigPanel;
