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/BaseForm.tsx
var BaseForm_exports = {};
__export(BaseForm_exports, {
  default: () => BaseForm_default
});
module.exports = __toCommonJS(BaseForm_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_ahooks = require("ahooks");
var import_pisellModal = __toESM(require("../../pisellModal"));
var import_useCtxActions = __toESM(require("../hooks/useCtxActions"));
var import_useDataSource = __toESM(require("../hooks/useDataSource"));
var import_useEngineContext = __toESM(require("../../../hooks/useEngineContext"));
var import_form = __toESM(require("../../form"));
var import_FormSettingProvider = __toESM(require("./provider/formSetting/FormSettingProvider"));
var import_page_header = __toESM(require("../../page-header"));
var import_utils = require("./utils");
var import_index = require("./index.less");
var BaseForm = (0, import_react.forwardRef)(
  (props, ref) => {
    var _a;
    const {
      title,
      description,
      dataSource,
      showTitle,
      showDescription,
      groupInfoPosition,
      variables,
      renderMode,
      children,
      currentValue,
      className,
      onDataSourceFinish,
      formatSubmitValues,
      formatInitialValues,
      customSubmit,
      leaveConfirmConfig,
      ...others
    } = props;
    const context = (0, import_useEngineContext.default)();
    const { setBeforeClose } = (0, import_useCtxActions.default)();
    const { getApp } = (_a = context.appHelper) == null ? void 0 : _a.utils;
    const formRef = (0, import_react.useRef)(null);
    const { get } = (0, import_useDataSource.default)();
    (0, import_react.useEffect)(() => {
      var _a2;
      const isDesignMode = props.__designMode === "design";
      if (isDesignMode) {
        (_a2 = formRef.current) == null ? void 0 : _a2.resetFields();
      }
    }, [dataSource, props.__designMode]);
    (0, import_react.useEffect)(() => {
      document.body.id = "body";
    }, []);
    (0, import_react.useImperativeHandle)(ref, () => formRef.current);
    (0, import_react.useEffect)(() => {
      if (["edit", "view"].includes(renderMode || "") && dataSource) {
        get == null ? void 0 : get.runAsync().then((res) => {
          var _a2;
          const formattedValues = formatInitialValues ? formatInitialValues(res) : res;
          (_a2 = formRef.current) == null ? void 0 : _a2.setFields((0, import_utils.transformFormObjToArrayWithUntouched)(formattedValues));
        });
      }
    }, [renderMode, dataSource]);
    const checkFormClose = (0, import_ahooks.useMemoizedFn)(async () => {
      var _a2;
      const isFormChanged = (_a2 = formRef.current) == null ? void 0 : _a2.isFieldsTouched();
      if (isFormChanged) {
        return new Promise((resolve) => {
          import_pisellModal.default.confirm({
            title: leaveConfirmConfig == null ? void 0 : leaveConfirmConfig.title,
            content: leaveConfirmConfig == null ? void 0 : leaveConfirmConfig.content,
            okText: leaveConfirmConfig == null ? void 0 : leaveConfirmConfig.okText,
            cancelText: leaveConfirmConfig == null ? void 0 : leaveConfirmConfig.cancelText,
            onOk: () => {
              var _a3;
              (_a3 = formRef.current) == null ? void 0 : _a3.resetFields();
              resolve(true);
            },
            onCancel: () => {
              resolve(false);
            }
          });
        });
      }
      return true;
    });
    (0, import_react.useEffect)(() => {
      if (leaveConfirmConfig == null ? void 0 : leaveConfirmConfig.enable) {
        setBeforeClose == null ? void 0 : setBeforeClose(checkFormClose);
      }
    }, [leaveConfirmConfig]);
    (0, import_react.useEffect)(() => {
      var _a2, _b;
      if (leaveConfirmConfig == null ? void 0 : leaveConfirmConfig.enable) {
        const history = (_b = (_a2 = getApp == null ? void 0 : getApp()) == null ? void 0 : _a2.history) == null ? void 0 : _b.instance;
        let unblock = history == null ? void 0 : history.block((location, action) => {
          var _a3;
          const isFormChanged = (_a3 = formRef.current) == null ? void 0 : _a3.isFieldsTouched();
          if (isFormChanged) {
            import_pisellModal.default.confirm({
              title: leaveConfirmConfig.title,
              content: leaveConfirmConfig.content,
              okText: leaveConfirmConfig.okText,
              cancelText: leaveConfirmConfig.cancelText,
              onOk: () => {
                unblock == null ? void 0 : unblock();
                if (action === "PUSH") {
                  history.push(location);
                } else if (action === "POP") {
                  history.go(-1);
                } else if (action === "REPLACE") {
                  history.replace(location);
                }
              },
              onCancel: () => {
              }
            });
            return false;
          }
          return true;
        });
        return () => {
          unblock == null ? void 0 : unblock();
        };
      }
    }, []);
    return /* @__PURE__ */ import_react.default.createElement(
      import_FormSettingProvider.default,
      {
        renderMode,
        groupInfoPosition,
        currentValue,
        size: others.size,
        onDataSourceFinish,
        formatSubmitValues,
        formatInitialValues,
        customSubmit
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_form.default,
        {
          ...others,
          className: (0, import_classnames.default)("pisell-lowcode-data-source-form", className),
          ref: formRef,
          preserve: true
        },
        /* @__PURE__ */ import_react.default.createElement(
          import_page_header.default,
          {
            title,
            showTitle,
            subTitle: description,
            showSubTitle: showDescription
          }
        ),
        children
      )
    );
  }
);
var BaseForm_default = BaseForm;
