import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
var _excluded = ["elements"];
import { useFields, useFormGroups } from './api';
import { typesWithElements } from './utils';
export default function useResolveElementReferences() {
  return resolveElementReferences({
    formGroup: useFormGroups(),
    field: useFields()
  });
}
export function resolveElementReferences(registry) {
  function resolveElement(element) {
    var _registryElements$fin;

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

  function resolveConfig(_ref) {
    var elements = _ref.elements,
        other = _objectWithoutProperties(_ref, _excluded);

    return _objectSpread(_objectSpread({}, other), {}, {
      elements: elements === null || elements === void 0 ? void 0 : elements.map(resolveElement)
    });
  }

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

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