"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const Slate = require("slate");
const reactBinding = require("@contember/react-binding");
const react = require("react");
const useGetReferenceEntityList = require("./useGetReferenceEntityList.cjs");
const useCreateElementReference = ({ monolithicReferencesMode, sortedBlocksRef, referenceDiscriminationField, referencesField, refreshBlocks }) => {
  const getReferenceList = useGetReferenceEntityList.useGetReferenceEntityList({ monolithicReferencesMode, sortedBlocksRef, referencesField });
  return useCreateElementReferenceInternal({
    referenceDiscriminationField,
    getReferenceList,
    refreshBlocks
  });
};
const useCreateElementReferenceInternal = ({ referenceDiscriminationField, getReferenceList, refreshBlocks }) => {
  const bindingOperations = reactBinding.useBindingOperations();
  return react.useCallback((editor, path, referenceDiscriminant, initialize) => {
    const referenceUuid = reactBinding.generateUuid();
    refreshBlocks();
    const references = getReferenceList(path);
    Slate.Editor.withoutNormalizing(editor, () => {
      bindingOperations.batchDeferredUpdates(() => {
        const innerInitialize = (getNewReference, options) => {
          getNewReference().getField("id").updateValue(referenceUuid);
          getNewReference().getField(referenceDiscriminationField).updateValue(referenceDiscriminant);
          initialize?.(getNewReference, options);
        };
        references.createNewEntity(innerInitialize);
      });
    });
    return references.getChildEntityById(referenceUuid);
  }, [bindingOperations, getReferenceList, referenceDiscriminationField, refreshBlocks]);
};
exports.useCreateElementReference = useCreateElementReference;
//# sourceMappingURL=useCreateElementReference.cjs.map
