"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.promise.js");
var _react = _interopRequireWildcard(require("react"));
var _useMergedState = _interopRequireDefault(require("rc-util/es/hooks/useMergedState"));
var _omit = _interopRequireDefault(require("omit.js"));
var _reactDom = require("react-dom");
var _warning = require("rc-util/es/warning");
var _scrollLocker = _interopRequireDefault(require("rc-util/lib/Dom/scrollLocker"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _modal = _interopRequireDefault(require("../modal"));
var _wauBaseForm = _interopRequireDefault(require("../wau-base-form"));
require("./style");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function ModalForm({
  children,
  trigger,
  onVisibleChange,
  modalProps,
  onFinish,
  title,
  width,
  ...rest
}) {
  var _context$locale, _context$locale2, _context$getPopupCont;
  const [visible, setVisible] = (0, _useMergedState.default)(!!rest.visible, {
    value: rest.visible,
    onChange: onVisibleChange
  });
  const [scrollLocker] = (0, _react.useState)(() => new _scrollLocker.default());
  (0, _warning.noteOnce)(
  // eslint-disable-next-line
  // @ts-expect-error
  !rest.footer || !(modalProps !== null && modalProps !== void 0 && modalProps.footer), 'ModalForm 是一个 ProForm 的特殊布局，如果想自定义按钮，请使用 submit.render 自定义。');
  const context = (0, _react.useContext)(_configProvider.default.ConfigContext);
  (0, _react.useEffect)(() => {
    if (visible) {
      var _scrollLocker$lock;
      scrollLocker === null || scrollLocker === void 0 || (_scrollLocker$lock = scrollLocker.lock) === null || _scrollLocker$lock === void 0 ? void 0 : _scrollLocker$lock.call(scrollLocker);
    } else {
      var _scrollLocker$unLock;
      scrollLocker === null || scrollLocker === void 0 || (_scrollLocker$unLock = scrollLocker.unLock) === null || _scrollLocker$unLock === void 0 ? void 0 : _scrollLocker$unLock.call(scrollLocker);
    }
    if (visible && rest.visible) {
      onVisibleChange === null || onVisibleChange === void 0 ? void 0 : onVisibleChange(true);
    }
  }, [visible]);
  (0, _react.useEffect)(() => () => {
    var _scrollLocker$unLock2;
    scrollLocker === null || scrollLocker === void 0 || (_scrollLocker$unLock2 = scrollLocker.unLock) === null || _scrollLocker$unLock2 === void 0 ? void 0 : _scrollLocker$unLock2.call(scrollLocker);
  });

  /** 设置 trigger 的情况下，懒渲染优化性能；使之可以直接配合表格操作等场景使用 */
  const isFirstRender = (0, _react.useRef)(!(modalProps !== null && modalProps !== void 0 && modalProps.forceRender));
  /**
   * IsFirstRender.current 或者 visible 为 true 的时候就渲染 不渲染能会造成一些问题, 比如再次打开值不对了 只有手动配置
   * drawerProps?.destroyOnClose 为 true 的时候才会每次关闭的时候删除 dom
   */
  const shouldRenderFormItems = (0, _react.useMemo)(() => {
    if (isFirstRender.current && visible === false) {
      return false;
    }
    if (visible === false && modalProps !== null && modalProps !== void 0 && modalProps.destroyOnClose) {
      return false;
    }
    return true;
  }, [visible, modalProps === null || modalProps === void 0 ? void 0 : modalProps.destroyOnClose]);

  /** 同步 props 和 本地的 ref */
  const formRef = (0, _react.useRef)();

  /** 如果 destroyOnClose ，重置一下表单 */
  (0, _react.useEffect)(() => {
    if (visible) {
      isFirstRender.current = false;
    }
    if (!visible && modalProps !== null && modalProps !== void 0 && modalProps.destroyOnClose) {
      var _formRef$current;
      (_formRef$current = formRef.current) === null || _formRef$current === void 0 ? void 0 : _formRef$current.resetFields();
    }
  }, [modalProps === null || modalProps === void 0 ? void 0 : modalProps.destroyOnClose, visible]);
  (0, _react.useImperativeHandle)(rest.formRef, () => formRef.current, [formRef.current]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _reactDom.createPortal)( /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauBaseForm.default, {
        layout: "vertical",
        ...(0, _omit.default)(rest, ['visible']),
        formRef: formRef,
        onFinish: async values => {
          if (!onFinish) {
            return;
          }
          const success = await onFinish(values);
          if (success) {
            var _formRef$current2;
            (_formRef$current2 = formRef.current) === null || _formRef$current2 === void 0 ? void 0 : _formRef$current2.resetFields();
            setVisible(false);
          }
        },
        submitter: {
          searchConfig: {
            submitText: (modalProps === null || modalProps === void 0 ? void 0 : modalProps.okText) || ((_context$locale = context.locale) === null || _context$locale === void 0 || (_context$locale = _context$locale.Modal) === null || _context$locale === void 0 ? void 0 : _context$locale.okText) || '确认',
            resetText: (modalProps === null || modalProps === void 0 ? void 0 : modalProps.cancelText) || ((_context$locale2 = context.locale) === null || _context$locale2 === void 0 || (_context$locale2 = _context$locale2.Modal) === null || _context$locale2 === void 0 ? void 0 : _context$locale2.cancelText) || '取消'
          },
          submitButtonProps: {
            type: (modalProps === null || modalProps === void 0 ? void 0 : modalProps.okType) || 'primary'
          },
          resetButtonProps: {
            onClick: e => {
              var _modalProps$onCancel;
              modalProps === null || modalProps === void 0 || (_modalProps$onCancel = modalProps.onCancel) === null || _modalProps$onCancel === void 0 ? void 0 : _modalProps$onCancel.call(modalProps, e);
              setVisible(false);
            }
          },
          ...rest.submitter
        },
        contentRender: (item, submitter) => {
          return /*#__PURE__*/(0, _jsxRuntime.jsx)(_modal.default, {
            title: title,
            getContainer: false,
            width: width || 800,
            bodyStyle: {
              paddingBottom: 0
            },
            ...modalProps,
            visible: visible,
            onCancel: e => {
              var _modalProps$onCancel2;
              setVisible(false);
              modalProps === null || modalProps === void 0 || (_modalProps$onCancel2 = modalProps.onCancel) === null || _modalProps$onCancel2 === void 0 ? void 0 : _modalProps$onCancel2.call(modalProps, e);
            },
            footer: submitter,
            children: shouldRenderFormItems ? item : null
          });
        },
        children: children
      })
    }), (context === null || context === void 0 || (_context$getPopupCont = context.getPopupContainer) === null || _context$getPopupCont === void 0 ? void 0 : _context$getPopupCont.call(context, document.body)) || document.body), trigger && /*#__PURE__*/_react.default.cloneElement(trigger, {
      ...trigger.props,
      onClick: e => {
        var _trigger$props, _trigger$props$onClic;
        setVisible(!visible);
        (_trigger$props = trigger.props) === null || _trigger$props === void 0 || (_trigger$props$onClic = _trigger$props.onClick) === null || _trigger$props$onClic === void 0 ? void 0 : _trigger$props$onClic.call(_trigger$props, e);
      }
    })]
  });
}
var _default = ModalForm;
exports.default = _default;