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/AddFieldModal/index.tsx
var AddFieldModal_exports = {};
__export(AddFieldModal_exports, {
  default: () => AddFieldModal_default
});
module.exports = __toCommonJS(AddFieldModal_exports);
var import_iconfont = __toESM(require("../../../iconfont"));
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_SelectField = __toESM(require("../SelectField"));
var import_index = require("./index.less");
var import_hooks = require("../../../../hooks");
var import_model = require("../../model");
var AddFieldModal = () => {
  const { state, dispatch } = (0, import_hooks.useSharedState)(import_model.Context);
  const { dataSource, defaultColumns } = state;
  const [form] = import_antd.Form.useForm();
  const [count, setCount] = (0, import_react.useState)(2);
  const [openModal, setOpenModal] = (0, import_react.useState)(false);
  const addField = (values) => {
    let field_name = values.field_name;
    if (defaultColumns.find((d) => d.dataIndex === field_name)) {
      import_antd.message.warning("This field already exists");
      return;
    }
    const newField = {
      title: field_name,
      dataIndex: field_name,
      editable: true,
      width: 200,
      field_type: values.field_type
    };
    dispatch({
      type: "setDefaultColumns",
      payload: [...defaultColumns, newField]
    });
    dispatch({
      type: "setDataSource",
      payload: dataSource.map((d) => {
        return {
          ...d,
          [field_name]: values.default_value
        };
      })
    });
    setCount(count + 1);
    onCloseModal();
  };
  const addFieldModal = (0, import_react.useMemo)(() => {
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Form,
      {
        layout: "vertical",
        form,
        onValuesChange: (e) => {
          console.log(e);
        },
        onFinish: addField
      },
      /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode-add-field-modal" }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          label: "Field Name",
          name: "field_name",
          rules: [
            {
              required: true,
              message: "Please enter a field name"
            }
          ]
        },
        /* @__PURE__ */ import_react.default.createElement(
          import_antd.Input,
          {
            placeholder: "Field name (optional)",
            className: "pisell-lowcode-header-search"
          }
        )
      ), /* @__PURE__ */ import_react.default.createElement(import_SelectField.default, null), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode-modal-footer" }, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode-add-describe" }, /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { type: "pi-Plus", className: "pisell-lowcode-icon" }), /* @__PURE__ */ import_react.default.createElement("span", { className: "pisell-lowcode-label" }, "Add description")), /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode-btns" }, /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: "pisell-lowcode-cancel",
          onClick: () => {
            onCloseModal();
          }
        },
        "Cancel"
      ), /* @__PURE__ */ import_react.default.createElement(
        "div",
        {
          className: "pisell-lowcode-submit",
          onClick: () => {
            form.submit();
          }
        },
        "Submit"
      ))))
    );
  }, [openModal, form]);
  const onCloseModal = () => {
    form.resetFields();
    setOpenModal(false);
  };
  return /* @__PURE__ */ import_react.default.createElement(
    import_antd.Popover,
    {
      content: addFieldModal,
      placement: "bottomRight",
      overlayInnerStyle: { padding: 0 },
      trigger: "click",
      overlayClassName: "pisell-lowcode-popover-wrap",
      onOpenChange: (e) => {
        if (!e) {
          onCloseModal();
        } else {
          setOpenModal(true);
        }
      },
      open: openModal
    },
    /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode-add-field-btn" }, /* @__PURE__ */ import_react.default.createElement(
      import_iconfont.default,
      {
        type: "pi-a-Property3Add",
        className: "pisell-lowcode-add-field-icon"
      }
    ))
  );
};
var AddFieldModal_default = AddFieldModal;
