"use client";

import React from "react";
import { AutoRegisteringComponentRenderer } from "../../AutoRegisteringComponentRenderer";
import { createComponentDefinition } from "../../utils/createComponentDefinition";
import { ComponentCategory } from "@/types/component";
import { PivotGridComponent } from "./PivotGridComponent";
import { PivotGridConfigPanel } from "./PivotGridConfigPanel";

/**
 * PivotGrid 컴포넌트 정의
 */
const PivotGridDefinition = createComponentDefinition({
  id: "pivot-grid",
  name: "피벗 그리드",
  nameEng: "PivotGrid Component",
  description: "다차원 데이터 분석을 위한 피벗 테이블 컴포넌트",
  category: ComponentCategory.DISPLAY,
  webType: "text",
  component: PivotGridComponent,
  defaultConfig: {
    dataSource: {
      type: "table",
      tableName: "",
    },
    fields: [],
    totals: {
      showRowGrandTotals: true,
      showColumnGrandTotals: true,
      showRowTotals: true,
      showColumnTotals: true,
    },
    style: {
      theme: "default",
      headerStyle: "default",
      cellPadding: "normal",
      borderStyle: "light",
      alternateRowColors: true,
      highlightTotals: true,
    },
    allowExpandAll: true,
    exportConfig: {
      excel: true,
    },
    height: "400px",
  },
  defaultSize: { width: 800, height: 500 },
  configPanel: PivotGridConfigPanel,
  icon: "BarChart3",
  tags: ["피벗", "분석", "집계", "그리드", "데이터"],
  version: "1.0.0",
  author: "개발팀",
  documentation: "",
});

/**
 * PivotGrid 렌더러
 * 자동 등록 시스템을 사용하여 컴포넌트를 레지스트리에 등록
 */
export class PivotGridRenderer extends AutoRegisteringComponentRenderer {
  static componentDefinition = PivotGridDefinition;

  render(): React.ReactElement {
    return (
      <PivotGridComponent
        {...this.props}
      />
    );
  }
}

// 자동 등록 실행
PivotGridRenderer.registerSelf();

// 강제 등록 (디버깅용)
if (typeof window !== "undefined") {
  setTimeout(() => {
    try {
      PivotGridRenderer.registerSelf();
    } catch (error) {
      console.error("❌ PivotGrid 강제 등록 실패:", error);
    }
  }, 1000);
}
