"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/editor/src/utils/block-selection-path.js
var block_selection_path_exports = {};
__export(block_selection_path_exports, {
  useGenerateBlockPath: () => useGenerateBlockPath,
  useRestoreBlockFromPath: () => useRestoreBlockFromPath
});
module.exports = __toCommonJS(block_selection_path_exports);
var import_data = require("@wordpress/data");
var import_element = require("@wordpress/element");
var import_block_editor = require("@wordpress/block-editor");
function useGenerateBlockPath() {
  const registry = (0, import_data.useRegistry)();
  return (0, import_element.useCallback)(
    (clientId) => {
      const { getBlock, getBlockParents, getBlockOrder } = registry.select(import_block_editor.store);
      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 = (0, import_data.useRegistry)();
  return (0, import_element.useCallback)(
    (path) => {
      if (!path || !Array.isArray(path) || path.length === 0) {
        return null;
      }
      const { getBlock, getBlockOrder } = registry.select(import_block_editor.store);
      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]
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useGenerateBlockPath,
  useRestoreBlockFromPath
});
//# sourceMappingURL=block-selection-path.cjs.map
