"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = withReview;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = require("react");

var _Review = _interopRequireDefault(require("./Review"));

var _jsxRuntime = require("react/jsx-runtime");

var _excluded = ["review", "onPostSubmit"],
    _excluded2 = ["review", "forceReview"];

function withReview(Form) {
  var ReviewAfterSubmit = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
    var review = _ref.review,
        onPostSubmit = _ref.onPostSubmit,
        other = (0, _objectWithoutProperties2["default"])(_ref, _excluded);

    var _useState = (0, _react.useState)(),
        _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
        submittedFormData = _useState2[0],
        setSubmittedFormData = _useState2[1];

    return submittedFormData !== undefined ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_Review["default"], (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, other), {}, {
      formData: submittedFormData
    })) : /*#__PURE__*/(0, _jsxRuntime.jsx)(Form, (0, _objectSpread2["default"])({
      ref: ref,
      onPostSubmit: /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
        var _len,
            args,
            _key,
            response,
            formData,
            _args = arguments;

        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                for (_len = _args.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
                  args[_key] = _args[_key];
                }

                _context.next = 3;
                return onPostSubmit === null || onPostSubmit === void 0 ? void 0 : onPostSubmit.apply(void 0, args);

              case 3:
                response = _context.sent;
                formData = args[2].formData;
                setSubmittedFormData(formData);
                return _context.abrupt("return", response);

              case 7:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))
    }, other));
  });
  return /*#__PURE__*/(0, _react.forwardRef)(function (_ref3, ref) {
    var _ref3$review = _ref3.review,
        review = _ref3$review === void 0 ? true : _ref3$review,
        _ref3$forceReview = _ref3.forceReview,
        forceReview = _ref3$forceReview === void 0 ? false : _ref3$forceReview,
        other = (0, _objectWithoutProperties2["default"])(_ref3, _excluded2);

    if (forceReview) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Review["default"], (0, _objectSpread2["default"])({
        ref: ref
      }, other));
    }

    if (review) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(ReviewAfterSubmit, (0, _objectSpread2["default"])({
        ref: ref
      }, other));
    }

    return /*#__PURE__*/(0, _jsxRuntime.jsx)(Form, (0, _objectSpread2["default"])({
      ref: ref
    }, other));
  });
}
//# sourceMappingURL=withReview.cjs.map