var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/table/Table/tableConfig/body/CellProvider/index.tsx
var CellProvider_exports = {};
__export(CellProvider_exports, {
  default: () => CellProvider_default
});
module.exports = __toCommonJS(CellProvider_exports);
var import_hooks = require("../../../../../../hooks");
var import_antd = require("antd");
var import_classnames = __toESM(require("classnames"));
var import_react = __toESM(require("react"));
var import_model = require("../../../../model");
var import_fields = require("../../../fields");
var import__ = require("../index");
var import_index = require("./index.less");
var CellProvider = (props) => {
  var _a, _b;
  const {
    title,
    editable,
    children,
    dataIndex,
    field_type,
    record,
    handleSave,
    render
  } = props;
  const { __is_group } = record || {};
  if (render || __is_group) {
    return children;
  }
  const [editing, setEditing] = (0, import_react.useState)(false);
  const inputRef = (0, import_react.useRef)(null);
  const form = (0, import_hooks.useSharedState)(import__.EditableContext);
  const { state, dispatch } = (0, import_hooks.useSharedState)(import_model.Context);
  const KEY = `${dataIndex}-${record == null ? void 0 : record.key}`;
  (0, import_react.useEffect)(() => {
    var _a2, _b2;
    if (editing) {
      (_b2 = (_a2 = inputRef.current) == null ? void 0 : _a2.focus) == null ? void 0 : _b2.call(_a2);
    }
  }, [editing]);
  const toggleEdit = () => {
    if (!import_fields.fieldMaps[field_type]) {
      return;
    }
    setEditing(!editing);
    form.setFieldsValue({ [dataIndex]: record[dataIndex] });
  };
  const save = async () => {
    try {
      const values = await form.validateFields();
      toggleEdit();
      handleSave({ ...record, ...values });
    } catch (errInfo) {
      console.log("Save failed:", errInfo);
    }
  };
  let childNode = children;
  const Com = (_a = (0, import_fields.getFieldByType)(field_type)) == null ? void 0 : _a.show;
  const EditCom = (_b = (0, import_fields.getFieldByType)(field_type)) == null ? void 0 : _b.edit;
  const editComRender = (0, import_react.useMemo)(() => {
    const NewEditCom = EditCom || import_antd.Input;
    return /* @__PURE__ */ import_react.default.createElement(
      NewEditCom,
      {
        ref: inputRef,
        onBlur: save,
        style: { height: "100%" },
        bordered: false
      }
    );
  }, [EditCom, save]);
  if (editable) {
    childNode = editing ? /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form.Item,
      {
        style: { margin: 0 },
        name: dataIndex,
        rules: [
          {
            required: true,
            message: `${title} is required.`
          }
        ]
      },
      editComRender
    ) : /* @__PURE__ */ import_react.default.createElement("div", { className: (0, import_classnames.default)("editable-cell-value-wrap") }, Com ? /* @__PURE__ */ import_react.default.createElement(Com, { ...props }) : children);
  }
  const focus = (0, import_react.useMemo)(() => {
    return state.currentCell === KEY;
  }, [KEY, state.currentCell]);
  (0, import_react.useEffect)(() => {
    if (!focus) {
      setEditing(false);
    }
  }, [focus]);
  const onFocus = () => {
    if (!state.setting.data.edit) {
      return;
    }
    if (record) {
      if (!focus) {
        dispatch({
          type: "setCurrentCell",
          payload: focus ? null : KEY
        });
      } else {
        toggleEdit();
      }
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(
    "div",
    {
      className: (0, import_classnames.default)("cell-provider", {
        ["focus"]: focus
      }),
      onClick: onFocus
    },
    childNode
  );
};
var CellProvider_default = CellProvider;
