"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const reactBinding = require("@contember/react-binding");
const react = require("react");
const Slate = require("slate");
const useBlockElementCache = ({ editor, blockList, sortableBy, contentField }) => {
  const [blockElementCache] = react.useState(() => /* @__PURE__ */ new WeakMap());
  const desugaredSortableByField = reactBinding.useDesugaredRelativeSingleField(sortableBy);
  const desugaredBlockContentField = reactBinding.useDesugaredRelativeSingleField(contentField);
  reactBinding.useEntityBeforeUpdate(
    react.useCallback(
      (getAccessor) => {
        const hasPendingOperations = !!editor.operations.length;
        if (hasPendingOperations) {
          return;
        }
        for (const blockEntity of getAccessor().getEntityList(blockList)) {
          const cachedElement = blockElementCache.get(blockEntity);
          if (cachedElement !== void 0) {
            continue;
          }
          const blockIndex = blockEntity.getRelativeSingleField(desugaredSortableByField).value;
          if (editor.children.length < blockIndex) {
            continue;
          }
          const previousNode = editor.children[blockIndex];
          const contentField2 = blockEntity.getRelativeSingleField(desugaredBlockContentField);
          const currentNode = editor.deserializeNodes(
            contentField2.value,
            `BlockEditor: The 'contentField' of a block contains invalid data.`
          )[0];
          if (Slate.Element.isElement(previousNode) && JSON.stringify(previousNode) === JSON.stringify(currentNode)) {
            blockElementCache.set(blockEntity, previousNode);
          }
        }
      },
      [blockElementCache, blockList, desugaredBlockContentField, desugaredSortableByField, editor]
    )
  );
  return blockElementCache;
};
exports.useBlockElementCache = useBlockElementCache;
//# sourceMappingURL=useBlockElementCache.cjs.map
