"use strict";

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

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

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 _useResolveElementReferences = _interopRequireDefault(require("./useResolveElementReferences"));

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

var _excluded = ["config"],
    _excluded2 = ["id", "rev", "layer"];

function withFormConfigLoader(Form) {
  var Resolver = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
    var _config;

    var config = _ref.config,
        other = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(Form, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
      ref: ref
    }, other), {}, {
      config: (_config = config, (0, _useResolveElementReferences["default"])()(_config))
    }));
  });
  var Loader = /*#__PURE__*/(0, _react.forwardRef)(function (_ref2, ref) {
    var id = _ref2.id,
        rev = _ref2.rev,
        layer = _ref2.layer,
        other = (0, _objectWithoutProperties2["default"])(_ref2, _excluded2);
    (0, _tinyInvariant["default"])(!(id && other.config), 'You should not use prop `id` with `config`');
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(Form, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({
      ref: ref,
      id: id,
      rev: rev,
      layer: layer
    }, other), {}, {
      config: (0, _api.useAtomicForm)(id, rev, layer)
    }));
  });
  return /*#__PURE__*/(0, _react.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__*/(0, _jsxRuntime.jsx)(WithFormConfigLoader, (0, _objectSpread2["default"])({
      ref: ref
    }, props));
  });
}
//# sourceMappingURL=withFormConfigLoader.cjs.map