"use strict";

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

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

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

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

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

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

var _api = require("./api");

var _utils = require("./utils");

var _reactIntl = require("react-intl");

var _excluded = ["onSubmit", "onPostSubmit", "onPreSubmit", "confirm", "formMetaData"];

var _default = function _default(_ref) {
  var _onSubmit;

  var onSubmit = _ref.onSubmit,
      onPostSubmit = _ref.onPostSubmit,
      onPreSubmit = _ref.onPreSubmit,
      confirm = _ref.confirm,
      formMetaData = _ref.formMetaData,
      other = (0, _objectWithoutProperties2["default"])(_ref, _excluded);

  var _useMutations = (0, _api.useMutations)(),
      submit = _useMutations.submit;

  var config = other.config;
  var intl = (0, _reactIntl.useIntl)();
  var locale = intl.locale;
  (_onSubmit = onSubmit) !== null && _onSubmit !== void 0 ? _onSubmit : onSubmit = submit;
  return (0, _objectSpread2["default"])({
    onSubmit: (0, _utils.chain)([onPreSubmit, /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      var _len,
          args,
          _key,
          _args$,
          captchaChallenge,
          formData,
          data,
          response,
          _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];
              }

              _args$ = args[0], captchaChallenge = _args$.captchaChallenge, formData = _args$.formData;
              data = (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
                status: 'SUBMITTED',
                form: {
                  id: config._id,
                  rev: config._rev,
                  lang: locale.split('-')[0]
                }
              }, formMetaData), {}, {
                values: formData,
                captchaChallenge: captchaChallenge
              });
              _context.next = 5;
              return onSubmit.apply(void 0, [data].concat(args));

            case 5:
              response = _context.sent;
              onPostSubmit === null || onPostSubmit === void 0 ? void 0 : onPostSubmit.apply(void 0, [response, data].concat(args));

            case 7:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))])
  }, other);
};

exports["default"] = _default;
//# sourceMappingURL=useSubmit.cjs.map