'use strict';

var core = require('@linaria/core');
var index$2 = require('./Columns/index.js');
var FormItem = require('./Columns/FormItem.js');
var style = require('./style.js');
var utils = require('./utils.js');
var index$1 = require('../Typo/index.js');
var React = require('react');
var index = require('../../coreX/OverflowTooltip/index.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

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] = React.useState();
  const width = Number.isNaN(Number(column.width)) ? column.width : column.width + "px";
  const headerOnChange = React.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" ? utils.increaseLastNumber(value, rowIndex) : value
        });
      });
      onChange == null ? void 0 : onChange(newData, column.key, shouldUpdateData);
    },
    [onChange, data, column]
  );
  const headerOnBlur = React.useCallback(() => {
    onBlur == null ? void 0 : onBlur(column.key, errMsg);
  }, [column.key, onBlur, errMsg]);
  const renderDefaultComponent = () => {
    if (!column.type)
      return null;
    const CellComponent = index$2.ColumnHeaderImpls[column.type];
    return /* @__PURE__ */ React__default.default.createElement(
      CellComponent,
      __spreadProps(__spreadValues({}, props), {
        column,
        onChange: headerOnChange,
        onBlur: headerOnBlur
      })
    );
  };
  const renderTitle = () => {
    const titleContent = typeof column.title === "string" ? /* @__PURE__ */ React__default.default.createElement(index, { tooltip: column.title, content: column.title }) : column.title;
    return /* @__PURE__ */ React__default.default.createElement("p", { className: core.cx(index$1.Typo.Label.l3_bold_title, style.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.default.createElement(
      FormItem.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.default.createElement(
    "div",
    {
      className: core.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()
  );
};

exports.BatchInputListHeaderCell = BatchInputListHeaderCell;
