"use strict";

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

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

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

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

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

var _utils = require("./utils");

var _excluded = ["elements"];

function useResolveElementReferences() {
  return resolveElementReferences({
    formGroup: (0, _api.useFormGroups)(),
    field: (0, _api.useFields)()
  });
}

function resolveElementReferences(registry) {
  function resolveElement(element) {
    var _registryElements$fin;

    var registryElements = registry[element.type];
    return registryElements ? (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, element), (_registryElements$fin = registryElements.find(function (registryElement) {
      return registryElement._id === element.id;
    }), resolveElementReferences(_registryElements$fin))) : element;
  }

  function resolveConfig(_ref) {
    var elements = _ref.elements,
        other = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
    return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, other), {}, {
      elements: elements === null || elements === void 0 ? void 0 : elements.map(resolveElement)
    });
  }

  function resolveElementReferences(config) {
    return _utils.typesWithElements.includes(config === null || config === void 0 ? void 0 : config.type) ? resolveConfig(config) : config;
  }

  return resolveElementReferences;
}
//# sourceMappingURL=useResolveElementReferences.cjs.map