import { cx } from '@linaria/core';
import { ColumnHeaderImpls } from './Columns/index.js';
import { FormItem } from './Columns/FormItem.js';
import { TitleStyle } from './style.js';
import { increaseLastNumber } from './utils.js';
import { Typo } from '../Typo/index.js';
import React__default, { useState, useCallback } from 'react';
import OverflowTooltip from '../../coreX/OverflowTooltip/index.js';

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const BatchInputListHeaderCell = (props) => {
  const { column, disabled, data, disableBatchFilling, onBlur, onChange } = props;
  const [errMsg, setErrMsg] = useState();
  const width = Number.isNaN(Number(column.width)) ? column.width : column.width + "px";
  const headerOnChange = useCallback(
    (value, shouldUpdateData = false) => {
      var _a;
      const err = (_a = column.validator) == null ? void 0 : _a.call(column, { value, isHeader: true });
      setErrMsg(err || void 0);
      const shouldAutoIncrease = column.type !== "password" && column.autoIncrease;
      const newData = data.map((cell, rowIndex) => {
        return __spreadProps(__spreadValues({}, cell), {
          [column.key]: shouldAutoIncrease && typeof value === "string" ? increaseLastNumber(value, rowIndex) : value
        });
      });
      onChange == null ? void 0 : onChange(newData, column.key, shouldUpdateData);
    },
    [onChange, data, column]
  );
  const headerOnBlur = useCallback(() => {
    onBlur == null ? void 0 : onBlur(column.key, errMsg);
  }, [column.key, onBlur, errMsg]);
  const renderDefaultComponent = () => {
    if (!column.type)
      return null;
    const CellComponent = ColumnHeaderImpls[column.type];
    return /* @__PURE__ */ React__default.createElement(
      CellComponent,
      __spreadProps(__spreadValues({}, props), {
        column,
        onChange: headerOnChange,
        onBlur: headerOnBlur
      })
    );
  };
  const renderTitle = () => {
    const titleContent = typeof column.title === "string" ? /* @__PURE__ */ React__default.createElement(OverflowTooltip, { tooltip: column.title, content: column.title }) : column.title;
    return /* @__PURE__ */ React__default.createElement("p", { className: cx(Typo.Label.l3_bold_title, TitleStyle) }, titleContent);
  };
  const renderSubTitle = () => {
    if (column.subTitleRender) {
      return column.subTitleRender({
        isHeader: true,
        disabled,
        onChange: headerOnChange,
        onBlur: headerOnBlur,
        formValue: data
      });
    }
    if (disableBatchFilling) {
      return null;
    }
    return /* @__PURE__ */ React__default.createElement(
      FormItem,
      {
        validateStatus: typeof errMsg === "string" && errMsg ? "error" : "",
        message: errMsg || void 0
      },
      // if the render prop is provided, use it to render the subTitle
      // otherwise, render the default component
      column.render ? column.render({
        isHeader: true,
        disabled,
        onChange: headerOnChange,
        onBlur: headerOnBlur,
        formValue: data
      }) : renderDefaultComponent()
    );
  };
  return /* @__PURE__ */ React__default.createElement(
    "div",
    {
      className: cx("eagle-table-form-cell", `align-${column.align || "left"}`),
      style: {
        maxWidth: column.width ? width : "",
        minWidth: column.width ? width : "",
        visibility: column.hidden ? "hidden" : "visible"
      }
    },
    renderTitle(),
    renderSubTitle()
  );
};

export { BatchInputListHeaderCell };
