"use strict";

require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.promise.js");
var _react = _interopRequireWildcard(require("react"));
var _warning = require("rc-util/es/warning");
var _wauBaseForm = _interopRequireDefault(require("../wau-base-form"));
var _stepFormContext = _interopRequireDefault(require("./step-form-context"));
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 StepForm({
  onFinish,
  step,
  formRef: propFormRef,
  ...restProps
}) {
  const formRef = (0, _react.useRef)();
  const context = (0, _react.useContext)(_stepFormContext.default);
  (0, _warning.noteOnce)(
  // eslint-disable-next-line
  // @ts-expect-error
  !restProps.submitter, 'StepForm 不包含提交按钮，请在 StepsForm 上');
  /** 重置 formRef */
  (0, _react.useImperativeHandle)(propFormRef, () => formRef.current);

  /** Dom 不存在的时候解除挂载 */
  (0, _react.useEffect)(() => {
    return () => {
      if (restProps.name) {
        context === null || context === void 0 ? void 0 : context.unRegForm(restProps.name);
      }
    };
  }, []);
  if (context && context !== null && context !== void 0 && context.formArrayRef) {
    context.formArrayRef.current[step || 0] = formRef;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauBaseForm.default, {
    formRef: formRef,
    onFinish: async values => {
      if (restProps.name) {
        context === null || context === void 0 ? void 0 : context.onFormFinish(restProps.name, values);
      }
      if (onFinish) {
        context === null || context === void 0 ? void 0 : context.setLoading(true);
        // 如果报错，直接抛出
        const success = await (onFinish === null || onFinish === void 0 ? void 0 : onFinish(values));
        if (success) {
          context === null || context === void 0 ? void 0 : context.next();
        }
        context === null || context === void 0 ? void 0 : context.setLoading(false);
        return;
      }
      context === null || context === void 0 ? void 0 : context.next();
    },
    layout: "vertical",
    ...restProps
  });
}
var _default = StepForm;
exports.default = _default;