"use strict";

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

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

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

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

var _react = require("react");

var _tinyInvariant = _interopRequireDefault(require("tiny-invariant"));

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

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

var _excluded = ["id", "rev", "dataId", "credentials", "dataIsDraft"];

function withLoader(Form) {
  var Loader = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
    var id = _ref.id,
        rev = _ref.rev,
        dataId = _ref.dataId,
        credentials = _ref.credentials,
        dataIsDraft = _ref.dataIsDraft,
        other = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
    (0, _tinyInvariant["default"])(!(dataId && other.formData), 'You should not use prop `dataId` with `formData`');

    var _useSubmittedFormData = (0, _api.useSubmittedFormData)(id, rev, credentials, dataId, dataIsDraft),
        formData = _useSubmittedFormData.values,
        form = _useSubmittedFormData.form,
        _id = _useSubmittedFormData.dataId,
        activeStep = _useSubmittedFormData.activeStep;

    var formId = String(form.id);
    var formRev = String(form.rev);

    if (/^fm_\d+_\d+$/.test(id)) {
      var formIdOfData = "fm_".concat(formId, "_").concat(formRev);
      (0, _tinyInvariant["default"])(String(id) === formIdOfData, 'Form data is linked to a different form identifier. You should not provide `id` with `dataId`');
    } else {
      (0, _tinyInvariant["default"])(id == null || String(id) === formId, 'Form data is linked to a different form id. You should not provide `id` with `dataId`');
      (0, _tinyInvariant["default"])(rev == null || String(rev) === formRev, 'Form data is linked to a different form revision. You should not provide `rev` with `dataId`');
      (0, _tinyInvariant["default"])(String(id).includes('latest'), 'Form data is linked to a different form identifier. You should not provide `id` with `dataId`');
    }

    return /*#__PURE__*/(0, _jsxRuntime.jsx)(Form, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
      ref: ref
    }, other), {}, {
      formData: formData,
      id: formId,
      rev: formRev,
      dataId: dataId,
      credentials: credentials,
      dataIsDraft: dataIsDraft,
      activeStep: activeStep
    }));
  });
  return /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
    var WithFormDataLoader = props.dataId ? Loader : Form;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(WithFormDataLoader, (0, _objectSpread2["default"])({
      ref: ref
    }, props));
  });
}
//# sourceMappingURL=withLoader.cjs.map