"use client";

import React from "react";
import { ComponentRendererProps } from "@/types/component";
import { DividerLineConfig } from "./types";

export interface DividerLineComponentProps extends ComponentRendererProps {
  config?: DividerLineConfig;
}

/**
 * DividerLine 컴포넌트
 * divider-line 컴포넌트입니다
 */
export const DividerLineComponent: React.FC<DividerLineComponentProps> = ({
  component,
  isDesignMode = false,
  isSelected = false,
  onClick,
  onDragStart,
  onDragEnd,
  config,
  className,
  style,
  ...props
}) => {
  // 컴포넌트 설정
  const componentConfig = {
    ...config,
    ...component.config,
  } as DividerLineConfig;

  // 스타일 계산 (위치는 RealtimePreviewDynamic에서 처리하므로 제외)
  const componentStyle: React.CSSProperties = {
    width: "100%",
    height: "100%",
    ...component.style,
    ...style,
    // width는 항상 100%로 고정 (부모 컨테이너가 gridColumns로 크기 제어)
    width: "100%",
  };

  // 디자인 모드 스타일
  if (isDesignMode) {
    componentStyle.border = "1px dashed #cbd5e1";
    componentStyle.borderColor = isSelected ? "#3b82f6" : "#cbd5e1";
  }

  // 이벤트 핸들러
  const handleClick = (e: React.MouseEvent) => {
    e.stopPropagation();
    onClick?.();
  };

  // DOM에 전달하면 안 되는 React-specific props 필터링
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  const {
    selectedScreen,
    onZoneComponentDrop,
    onZoneClick,
    componentConfig: _componentConfig,
    component: _component,
    isSelected: _isSelected,
    onClick: _onClick,
    onDragStart: _onDragStart,
    onDragEnd: _onDragEnd,
    size: _size,
    position: _position,
    style: _style,
    screenId: _screenId,
    tableName: _tableName,
    onRefresh: _onRefresh,
    onClose: _onClose,
    // 추가된 props 필터링
    webType: _webType,
    autoGeneration: _autoGeneration,
    isInteractive: _isInteractive,
    formData: _formData,
    onFormDataChange: _onFormDataChange,
    menuId: _menuId,
    menuObjid: _menuObjid,
    onSave: _onSave,
    userId: _userId,
    userName: _userName,
    companyCode: _companyCode,
    isInModal: _isInModal,
    readonly: _readonly,
    originalData: _originalData,
    _originalData: __originalData,
    _initialData: __initialData,
    _groupedData: __groupedData,
    allComponents: _allComponents,
    onUpdateLayout: _onUpdateLayout,
    selectedRows: _selectedRows,
    selectedRowsData: _selectedRowsData,
    onSelectedRowsChange: _onSelectedRowsChange,
    sortBy: _sortBy,
    sortOrder: _sortOrder,
    tableDisplayData: _tableDisplayData,
    flowSelectedData: _flowSelectedData,
    flowSelectedStepId: _flowSelectedStepId,
    onFlowSelectedDataChange: _onFlowSelectedDataChange,
    onConfigChange: _onConfigChange,
    refreshKey: _refreshKey,
    flowRefreshKey: _flowRefreshKey,
    onFlowRefresh: _onFlowRefresh,
    isPreview: _isPreview,
    groupedData: _groupedData,
    ...domProps
  } = props as any;

  return (
    <div style={componentStyle} className={className} {...domProps}>
      {/* 라벨 렌더링 */}
      {component.label && component.style?.labelDisplay !== false && (
        <label
          style={{
            position: "absolute",
            top: "-25px",
            left: "0px",
            fontSize: component.style?.labelFontSize || "14px",
            color: component.style?.labelColor || "#64748b",
            fontWeight: "500",
          }}
        >
          {component.label}
          {component.required && <span style={{ color: "#ef4444" }}>*</span>}
        </label>
      )}

      <div
        style={{
          width: "100%",
          height: "100%",
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
        }}
        onClick={handleClick}
        onDragStart={onDragStart}
        onDragEnd={onDragEnd}
      >
        {componentConfig.dividerText ? (
          <div
            style={{
              display: "flex",
              alignItems: "center",
              width: "100%",
              gap: "12px",
            }}
          >
            <div
              style={{
                flex: 1,
                height: "1px",
                backgroundColor: componentConfig.color || "#d1d5db",
              }}
            />
            <span
              style={{
                color: componentConfig.textColor || "#6b7280",
                fontSize: "14px",
                whiteSpace: "nowrap",
              }}
            >
              {componentConfig.dividerText}
            </span>
            <div
              style={{
                flex: 1,
                height: "1px",
                backgroundColor: componentConfig.color || "#d1d5db",
              }}
            />
          </div>
        ) : (
          <div
            style={{
              width: "100%",
              height: componentConfig.thickness || "1px",
              backgroundColor: componentConfig.color || "#d1d5db",
              borderRadius: componentConfig.rounded ? "999px" : "0",
            }}
          />
        )}
      </div>
    </div>
  );
};

/**
 * DividerLine 래퍼 컴포넌트
 * 추가적인 로직이나 상태 관리가 필요한 경우 사용
 */
export const DividerLineWrapper: React.FC<DividerLineComponentProps> = (props) => {
  return <DividerLineComponent {...props} />;
};
