'use strict';

var core = require('@linaria/core');
var index$1 = require('./Columns/index.js');
var FormItem = require('./Columns/FormItem.js');
var types = require('./types.js');
var index = require('../Typo/index.js');
var React = require('react');

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 TableFormBodyCell = (props) => {
  const {
    column,
    index: rowIndex,
    data,
    latestData,
    validateTriggerType,
    isRowError,
    getRowValidateResult,
    disabled,
    onChange,
    onBlur,
    validateAllCount,
    error,
    onValidate
  } = props;
  const [validateResult, setValidateResult] = React.useState();
  const [isTouched, setIsTouched] = React.useState(false);
  const width = typeof column.width === "number" ? column.width + "px" : column.width;
  const isCellErrorStyle = Boolean(
    isTouched && error || (validateResult == null ? void 0 : validateResult.isError) || isRowError
  );
  const triggerValidate = React.useCallback(
    (currentValue) => {
      var _a;
      const value = currentValue != null ? currentValue : data[rowIndex][column.key];
      const rowData = __spreadProps(__spreadValues({}, data[rowIndex]), { [column.key]: value });
      const rowValidateRes = getRowValidateResult(rowData);
      if (rowValidateRes) {
        return;
      }
      if (column.validator) {
        const result = (_a = column.validator) == null ? void 0 : _a.call(column, {
          value,
          rowIndex,
          rowData: data[rowIndex]
        });
        const isError = result ? typeof result === "string" : false;
        setValidateResult({ msg: result || "", isError });
        onValidate == null ? void 0 : onValidate(`${column.key}${rowIndex}`, !isError);
        return;
      }
    },
    [data, rowIndex, column, getRowValidateResult, onValidate]
  );
  React.useEffect(() => {
    if (validateAllCount > 0) {
      setIsTouched(true);
      triggerValidate();
    }
  }, [validateAllCount, triggerValidate, setIsTouched]);
  const _onChange = (value, data2) => {
    const newData = data2.map(
      (row, i) => i === rowIndex ? __spreadProps(__spreadValues({}, row), { [column.key]: value }) : row
    );
    onChange == null ? void 0 : onChange(newData, rowIndex, column.key);
    if (validateTriggerType === types.ValidateTriggerType.Normal && isTouched || validateTriggerType === types.ValidateTriggerType.Aggressive) {
      triggerValidate(value);
    }
  };
  const _onBlur = React.useCallback(() => {
    setIsTouched(true);
    triggerValidate();
    onBlur == null ? void 0 : onBlur(data, rowIndex, column.key);
  }, [rowIndex, column, onBlur, data, triggerValidate, setIsTouched]);
  const renderDefaultComponent = () => {
    if (!column.type)
      return null;
    const CellComponent = index$1.ColumnBodyImpls[column.type];
    return /* @__PURE__ */ React__default.default.createElement(
      CellComponent,
      {
        disabled,
        data,
        index: rowIndex,
        latestData,
        column,
        visible: props.visible,
        error: isCellErrorStyle,
        onChange: (val) => {
          _onChange(val, data);
        },
        onBlur: _onBlur
      }
    );
  };
  const Cell = column.render ? column.render({
    isHeader: false,
    disabled,
    placeholder: props.placeholderValue || latestData[rowIndex][column.key],
    value: data[rowIndex][column.key],
    onChange: (val) => {
      _onChange(val, data);
    },
    onBlur: _onBlur,
    rowIndex,
    error: isCellErrorStyle,
    formValue: data
  }) : renderDefaultComponent();
  const CellDescription = React.useMemo(() => {
    var _a;
    if (column.type === "text")
      return null;
    const Result = ((_a = column.renderDescription) == null ? void 0 : _a.call(column, {
      rowIndex,
      rowData: data[rowIndex],
      latestData
    })) || null;
    return typeof Result === "string" ? /* @__PURE__ */ React__default.default.createElement("p", { className: core.cx(index.Typo.Label.l4_regular, "cell-description") }, Result) : Result;
  }, [rowIndex, data, latestData, column]);
  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"
      }
    },
    /* @__PURE__ */ React__default.default.createElement(
      FormItem.FormItem,
      {
        validateStatus: isCellErrorStyle ? "error" : "",
        message: isRowError ? "" : (isTouched ? error : void 0) || (validateResult == null ? void 0 : validateResult.msg)
      },
      Cell
    ),
    CellDescription
  );
};

exports.TableFormBodyCell = TableFormBodyCell;
