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/dataSourceComponents/dataSourceForm/submitButton/index.tsx
var submitButton_exports = {};
__export(submitButton_exports, {
  default: () => submitButton_default
});
module.exports = __toCommonJS(submitButton_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_pisellToast = __toESM(require("../../../pisellToast"));
var import_pisellModal = __toESM(require("../../../pisellModal"));
var import_formItem = __toESM(require("../formItem"));
var import_useFormSetting = __toESM(require("../provider/hooks/useFormSetting"));
var import_useCtxActions = __toESM(require("../../hooks/useCtxActions"));
var import_useDataSource = __toESM(require("../../hooks/useDataSource"));
var import_utils = require("../utils");
var import_useFormSettingParams = __toESM(require("../../hooks/useFormSettingParams"));
var import_index = require("./index.less");
var import_serve = require("../serve");
var SubmitButton = (props) => {
  const {
    children,
    status,
    type,
    confirmConfig,
    toastConfig,
    refreshData,
    afterSubmitType,
    afterSubmitModalConfig,
    afterSubmitNotificationConfig,
    childrenString
  } = props;
  const [loading, setLoading] = (0, import_react.useState)(false);
  const [api, contextHolder] = import_antd.notification.useNotification();
  const { scope, target } = (0, import_useFormSettingParams.default)();
  const form = import_antd.Form.useFormInstance();
  const { renderMode, onDataSourceFinish, formatSubmitValues, customSubmit, beforeSubmit, scene, sceneRequestId } = (0, import_useFormSetting.default)();
  const { setVisible, refreshTableData } = (0, import_useCtxActions.default)();
  const { create, update, dataSource } = (0, import_useDataSource.default)();
  const isDisabled = status === "disabled";
  const onSubmit = async (values) => {
    setLoading(true);
    try {
      if (dataSource == null ? void 0 : dataSource.fromDeviceSettingMeta) {
        return await (0, import_serve.updateDeviceSetting)({
          scene,
          sceneRequestId: sceneRequestId || "",
          data: values
        });
      }
      if (dataSource == null ? void 0 : dataSource.fromSettingMeta) {
        return await (0, import_serve.updateTenantSetting)({
          scope,
          target,
          data: values
        });
      }
      if (customSubmit) {
        return await customSubmit(values);
      }
      if (renderMode === "edit") {
        return await update.runAsync(values);
      } else if (renderMode === "add") {
        const res = await create.runAsync(values);
        form == null ? void 0 : form.resetFields();
        return res;
      }
    } finally {
      setLoading(false);
    }
  };
  const afterSubmitFuncMap = {
    close: () => {
      form.resetFields();
      setVisible == null ? void 0 : setVisible(false);
    },
    showModal: () => {
      import_pisellModal.default.success({
        title: afterSubmitModalConfig == null ? void 0 : afterSubmitModalConfig.title,
        content: afterSubmitModalConfig == null ? void 0 : afterSubmitModalConfig.content,
        okText: afterSubmitModalConfig == null ? void 0 : afterSubmitModalConfig.okText,
        onOk: afterSubmitModalConfig == null ? void 0 : afterSubmitModalConfig.onOk
      });
    },
    showNotification: () => {
      api.open({
        message: afterSubmitNotificationConfig == null ? void 0 : afterSubmitNotificationConfig.title,
        description: afterSubmitNotificationConfig == null ? void 0 : afterSubmitNotificationConfig.content,
        duration: afterSubmitNotificationConfig == null ? void 0 : afterSubmitNotificationConfig.duration,
        placement: afterSubmitNotificationConfig == null ? void 0 : afterSubmitNotificationConfig.placement
      });
    }
  };
  const validateFields = () => {
    return form.validateFields();
  };
  const handleValideFieldsSubmit = async (values) => {
    try {
      await validateFields();
      const allValues = form.getFieldsValue(true);
      if (allValues.id) {
        values.id = allValues.id;
      }
      if ((toastConfig == null ? void 0 : toastConfig.enable) && !(toastConfig == null ? void 0 : toastConfig.showAfterSubmit)) {
        (0, import_pisellToast.default)({
          content: toastConfig.title
        });
      }
      const filteredValues = (0, import_utils.filterSystemFields)(allValues);
      const formattedValues = formatSubmitValues ? formatSubmitValues(filteredValues) : filteredValues;
      if (beforeSubmit) {
        const canSubmit = await beforeSubmit(formattedValues);
        if (!canSubmit) {
          return;
        }
      }
      const result = await onSubmit(formattedValues);
      if (onDataSourceFinish) {
        onDataSourceFinish(formattedValues, result, form);
      }
      if (refreshData) {
        refreshTableData == null ? void 0 : refreshTableData();
      }
      if ((toastConfig == null ? void 0 : toastConfig.enable) && (toastConfig == null ? void 0 : toastConfig.showAfterSubmit)) {
        (0, import_pisellToast.default)({
          content: toastConfig.title
        });
      }
      if (afterSubmitType) {
        const afterSubmitFunc = afterSubmitFuncMap[afterSubmitType];
        if (afterSubmitFunc) {
          afterSubmitFunc();
        }
      }
      (0, import_utils.setFormFieldsUntouched)(form);
    } catch (error) {
      (0, import_pisellToast.default)({
        content: `${error.message}`
      });
    }
  };
  const onSubmitClick = async () => {
    const values = await validateFields();
    if (confirmConfig == null ? void 0 : confirmConfig.enable) {
      return import_pisellModal.default.confirm({
        title: confirmConfig.title,
        content: confirmConfig.content,
        okText: confirmConfig.okText,
        cancelText: confirmConfig.cancelText,
        onOk: () => {
          handleValideFieldsSubmit(values);
        }
      });
    }
    handleValideFieldsSubmit(values);
  };
  if (status === "hidden" || renderMode === "view") {
    return null;
  }
  return /* @__PURE__ */ import_react.default.createElement(import_formItem.default, null, /* @__PURE__ */ import_react.default.createElement("div", { className: "pisell-lowcode-data-source-form-submit-button-wrapper" }, /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      type,
      size: "large",
      loading,
      onClick: onSubmitClick,
      disabled: isDisabled,
      htmlType: "submit"
    },
    childrenString
  ), contextHolder));
};
var submitButton_default = SubmitButton;
