import { BindingError, useEntityList } from "@contember/react-binding";
import { useCallback } from "react";
import { useConstantValueInvariant } from "@contember/react-utils";
const useGetReferenceEntityList = ({
  referencesField,
  sortedBlocksRef,
  monolithicReferencesMode
}) => {
  useConstantValueInvariant(referencesField);
  useConstantValueInvariant(monolithicReferencesMode);
  if (!referencesField) {
    return useCallback(() => {
      throw new BindingError(
        `BlockEditor: trying to get or insert a referenced element but referencing has not been correctly set up. Check the BlockEditor props.`
      );
    }, []);
  }
  return monolithicReferencesMode ? useGetMonolithicReferenceList({ referencesField }) : useGetPerBlockReferenceList({ referencesField, sortedBlocksRef });
};
const useGetMonolithicReferenceList = ({ referencesField }) => {
  const referenceList = useEntityList(referencesField);
  return useCallback(() => referenceList, [referenceList]);
};
const useGetPerBlockReferenceList = ({ referencesField, sortedBlocksRef }) => {
  return useCallback((targetPath) => {
    const blockIndex = targetPath[0];
    const containingBlock = sortedBlocksRef.current[blockIndex];
    if (!containingBlock) {
      throw new BindingError();
    }
    return containingBlock.getEntityList(referencesField);
  }, [referencesField, sortedBlocksRef]);
};
export {
  useGetReferenceEntityList
};
//# sourceMappingURL=useGetReferenceEntityList.js.map
