import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["id", "rev", "dataId", "credentials", "dataIsDraft"];
import { forwardRef } from 'react';
import invariant from 'tiny-invariant';
import { useSubmittedFormData } from '../api';
import { jsx as _jsx } from "react/jsx-runtime";
export default function withLoader(Form) {
  var Loader = /*#__PURE__*/forwardRef(function (_ref, ref) {
    var id = _ref.id,
        rev = _ref.rev,
        dataId = _ref.dataId,
        credentials = _ref.credentials,
        dataIsDraft = _ref.dataIsDraft,
        other = _objectWithoutProperties(_ref, _excluded);

    invariant(!(dataId && other.formData), 'You should not use prop `dataId` with `formData`');

    var _useSubmittedFormData = 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);
      invariant(String(id) === formIdOfData, 'Form data is linked to a different form identifier. You should not provide `id` with `dataId`');
    } else {
      invariant(id == null || String(id) === formId, 'Form data is linked to a different form id. You should not provide `id` with `dataId`');
      invariant(rev == null || String(rev) === formRev, 'Form data is linked to a different form revision. You should not provide `rev` with `dataId`');
      invariant(String(id).includes('latest'), 'Form data is linked to a different form identifier. You should not provide `id` with `dataId`');
    }

    return /*#__PURE__*/_jsx(Form, _objectSpread(_objectSpread({
      ref: ref
    }, other), {}, {
      formData: formData,
      id: formId,
      rev: formRev,
      dataId: dataId,
      credentials: credentials,
      dataIsDraft: dataIsDraft,
      activeStep: activeStep
    }));
  });
  return /*#__PURE__*/forwardRef(function (props, ref) {
    var WithFormDataLoader = props.dataId ? Loader : Form;
    return /*#__PURE__*/_jsx(WithFormDataLoader, _objectSpread({
      ref: ref
    }, props));
  });
}
//# sourceMappingURL=withLoader.js.map