// packages/editor/src/utils/block-selection-path.js
import { useRegistry } from "@wordpress/data";
import { useCallback } from "@wordpress/element";
import { store as blockEditorStore } from "@wordpress/block-editor";
function useGenerateBlockPath() {
  const registry = useRegistry();
  return useCallback(
    (clientId) => {
      const { getBlock, getBlockParents, getBlockOrder } = registry.select(blockEditorStore);
      const block = getBlock(clientId);
      if (!block) {
        return null;
      }
      const parents = getBlockParents(clientId);
      const path = [];
      const hierarchy = [...parents, clientId];
      for (let i = 0; i < hierarchy.length; i++) {
        const currentClientId = hierarchy[i];
        const currentBlock = getBlock(currentClientId);
        if (!currentBlock) {
          return null;
        }
        const parentClientId = i > 0 ? hierarchy[i - 1] : "";
        const siblings = getBlockOrder(parentClientId);
        const index = siblings.indexOf(currentClientId);
        if (index === -1) {
          return null;
        }
        path.push({
          blockName: currentBlock.name,
          index,
          // Store a simple content hash for verification (first 100 chars of serialized content)
          contentHash: JSON.stringify(
            currentBlock.attributes
          ).slice(0, 100)
        });
      }
      return path;
    },
    [registry]
  );
}
function useRestoreBlockFromPath() {
  const registry = useRegistry();
  return useCallback(
    (path) => {
      if (!path || !Array.isArray(path) || path.length === 0) {
        return null;
      }
      const { getBlock, getBlockOrder } = registry.select(blockEditorStore);
      let currentParentId = "";
      for (let i = 0; i < path.length; i++) {
        const step = path[i];
        const siblings = getBlockOrder(currentParentId);
        if (step.index >= siblings.length) {
          return null;
        }
        const candidateClientId = siblings[step.index];
        const candidateBlock = getBlock(candidateClientId);
        if (!candidateBlock) {
          return null;
        }
        if (candidateBlock.name !== step.blockName) {
          return null;
        }
        if (i === path.length - 1) {
          return candidateClientId;
        }
        currentParentId = candidateClientId;
      }
      return null;
    },
    [registry]
  );
}
export {
  useGenerateBlockPath,
  useRestoreBlockFromPath
};
//# sourceMappingURL=block-selection-path.js.map
