import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["config"],
    _excluded2 = ["id", "rev", "layer"];
import { forwardRef } from 'react';
import invariant from 'tiny-invariant';
import { useAtomicForm } from './api';
import useResolveElementReferences from './useResolveElementReferences';
import { jsx as _jsx } from "react/jsx-runtime";
export default function withFormConfigLoader(Form) {
  var Resolver = /*#__PURE__*/forwardRef(function (_ref, ref) {
    var _config;

    var config = _ref.config,
        other = _objectWithoutProperties(_ref, _excluded);

    return /*#__PURE__*/_jsx(Form, _objectSpread(_objectSpread({
      ref: ref
    }, other), {}, {
      config: (_config = config, useResolveElementReferences()(_config))
    }));
  });
  var Loader = /*#__PURE__*/forwardRef(function (_ref2, ref) {
    var id = _ref2.id,
        rev = _ref2.rev,
        layer = _ref2.layer,
        other = _objectWithoutProperties(_ref2, _excluded2);

    invariant(!(id && other.config), 'You should not use prop `id` with `config`');
    return /*#__PURE__*/_jsx(Form, _objectSpread(_objectSpread({
      ref: ref,
      id: id,
      rev: rev,
      layer: layer
    }, other), {}, {
      config: useAtomicForm(id, rev, layer)
    }));
  });
  return /*#__PURE__*/forwardRef(function (props, ref) {
    var WithFormConfigLoader = // Load config and resolve elements
    props.id ? Loader : // Optionally resolve directly provided config
    props.resolve ? Resolver : Form;
    return /*#__PURE__*/_jsx(WithFormConfigLoader, _objectSpread({
      ref: ref
    }, props));
  });
}
//# sourceMappingURL=withFormConfigLoader.js.map