import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["review", "onPostSubmit"],
    _excluded2 = ["review", "forceReview"];
import { forwardRef, useState } from 'react';
import Review from './Review';
import { jsx as _jsx } from "react/jsx-runtime";
export default function withReview(Form) {
  var ReviewAfterSubmit = /*#__PURE__*/forwardRef(function (_ref, ref) {
    var review = _ref.review,
        onPostSubmit = _ref.onPostSubmit,
        other = _objectWithoutProperties(_ref, _excluded);

    var _useState = useState(),
        _useState2 = _slicedToArray(_useState, 2),
        submittedFormData = _useState2[0],
        setSubmittedFormData = _useState2[1];

    return submittedFormData !== undefined ? /*#__PURE__*/_jsx(Review, _objectSpread(_objectSpread({}, other), {}, {
      formData: submittedFormData
    })) : /*#__PURE__*/_jsx(Form, _objectSpread({
      ref: ref,
      onPostSubmit: /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
        var _len,
            args,
            _key,
            response,
            formData,
            _args = arguments;

        return _regeneratorRuntime.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__*/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 = _objectWithoutProperties(_ref3, _excluded2);

    if (forceReview) {
      return /*#__PURE__*/_jsx(Review, _objectSpread({
        ref: ref
      }, other));
    }

    if (review) {
      return /*#__PURE__*/_jsx(ReviewAfterSubmit, _objectSpread({
        ref: ref
      }, other));
    }

    return /*#__PURE__*/_jsx(Form, _objectSpread({
      ref: ref
    }, other));
  });
}
//# sourceMappingURL=withReview.js.map