import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["onSubmit", "onPostSubmit", "onPreSubmit", "confirm", "formMetaData"];
import { useMutations } from './api';
import { chain } from './utils';
import { useIntl } from 'react-intl';
export default (function (_ref) {
  var _onSubmit;

  var onSubmit = _ref.onSubmit,
      onPostSubmit = _ref.onPostSubmit,
      onPreSubmit = _ref.onPreSubmit,
      confirm = _ref.confirm,
      formMetaData = _ref.formMetaData,
      other = _objectWithoutProperties(_ref, _excluded);

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

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

              _args$ = args[0], captchaChallenge = _args$.captchaChallenge, formData = _args$.formData;
              data = _objectSpread(_objectSpread({
                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);
});
//# sourceMappingURL=useSubmit.js.map