{
  "version": 3,
  "sources": ["../../src/awareness/block-lookup.ts"],
  "sourcesContent": ["import { useSelect } from '@wordpress/data';\nimport { Y } from '@wordpress/sync';\n// @ts-expect-error `@wordpress/block-editor` does not expose type declarations for its entry point.\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport type { AbsoluteBlockIndexPath } from '../types';\nimport { unlock } from '../lock-unlock';\n\n/**\n * A block as represented in the block-editor store's client ID tree.\n *\n * This is a minimal interface covering only the fields used by RTC awareness.\n */\nexport type EditorStoreBlock = {\n\tclientId: string;\n\tinnerBlocks: EditorStoreBlock[];\n};\n\n/**\n * Find the block Y.Map that contains a nested Yjs type.\n *\n * Rich-text attributes are often stored directly at attributes.content, but\n * blocks can also store rich text deeper inside object or array attributes.\n * Walk upward until we find the block map instead of assuming a fixed parent\n * depth.\n *\n * @param yType - The nested Yjs type to start from.\n * @return The containing block Y.Map, or null if no block ancestor exists.\n */\nexport function getContainingBlockYMap(\n\tyType: Y.AbstractType< any >\n): Y.Map< unknown > | null {\n\tlet current: Y.AbstractType< any > | null = yType;\n\n\twhile ( current ) {\n\t\tconst parent = current.parent;\n\n\t\tif (\n\t\t\tparent instanceof Y.Map &&\n\t\t\tparent.parent instanceof Y.Array &&\n\t\t\tparent.get( 'clientId' ) !== undefined &&\n\t\t\tparent.get( 'innerBlocks' ) instanceof Y.Array\n\t\t) {\n\t\t\treturn parent;\n\t\t}\n\n\t\tcurrent = parent instanceof Y.AbstractType ? parent : null;\n\t}\n\n\treturn null;\n}\n\n/**\n * Given a Y.Map within a Ydoc, traverse up the Yjs block tree to compute the\n * index path from the root.\n *\n * For example, the second inner block of the first root block returns [0, 1].\n *\n * @param yType - The Yjs block Y.Map to start from.\n * @return The index path from root, or null if traversal fails.\n */\nexport function getBlockPathInYdoc(\n\tyType: Y.Map< unknown >\n): AbsoluteBlockIndexPath | null {\n\tconst path: AbsoluteBlockIndexPath = [];\n\tlet current: Y.Map< unknown > = yType;\n\n\twhile ( current ) {\n\t\tconst parentArray = current.parent;\n\n\t\tif ( ! parentArray || ! ( parentArray instanceof Y.Array ) ) {\n\t\t\treturn null;\n\t\t}\n\n\t\t// Find index of current block in its parent array.\n\t\tlet index = -1;\n\t\tfor ( let i = 0; i < parentArray.length; i++ ) {\n\t\t\tif ( parentArray.get( i ) === current ) {\n\t\t\t\tindex = i;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif ( index === -1 ) {\n\t\t\treturn null;\n\t\t}\n\n\t\tpath.unshift( index );\n\n\t\t// Walk up: is the parent array's parent a block Y.Map or the root?\n\t\tconst grandparent = parentArray.parent;\n\t\tif (\n\t\t\tgrandparent instanceof Y.Map &&\n\t\t\tgrandparent.get( 'clientId' ) !== undefined\n\t\t) {\n\t\t\tcurrent = grandparent; // It's a block, keep going.\n\t\t} else {\n\t\t\tbreak; // It's the root map, done.\n\t\t}\n\t}\n\n\treturn path;\n}\n\n/**\n * Navigate the block-editor store's block tree by an index path\n * and return the local block's clientId.\n *\n * @param path   - The index path, e.g. [0, 1] for blocks[0].innerBlocks[1].\n * @param blocks - The tree of block-editor store post contentblocks.\n * @return The local block clientId, or null if the path is invalid.\n */\nexport function resolveBlockClientIdByPath(\n\tpath: AbsoluteBlockIndexPath,\n\tblocks: EditorStoreBlock[]\n): string | null {\n\tif ( path.length === 0 ) {\n\t\treturn null;\n\t}\n\n\tfor ( let i = 0; i < path.length; i++ ) {\n\t\tconst block = blocks[ path[ i ] ];\n\t\tif ( ! block ) {\n\t\t\treturn null;\n\t\t}\n\t\tif ( i === path.length - 1 ) {\n\t\t\treturn block.clientId;\n\t\t}\n\t\tblocks = block.innerBlocks;\n\t}\n\treturn null;\n}\n\n/**\n * Find the post content blocks to use as the navigation root.\n *\n * In template mode, the block tree contains template parts wrapping a\n * core/post-content block. The Yjs document only stores the post content\n * blocks, so we need to find the core/post-content block and use\n * getClientIdsTree(clientId) to retrieve its inner blocks from the store.\n *\n * Uses the private getClientIdsTree selector which depends only on\n * state.blocks.order, avoiding unnecessary re-renders when block\n * attributes change (which would happen with getBlocks()).\n *\n * @return The blocks that correspond to the Yjs document root.\n */\nexport function usePostContentBlocks(): EditorStoreBlock[] {\n\treturn useSelect( ( select ) => {\n\t\tconst { getBlocksByName, getClientIdsTree } = unlock(\n\t\t\tselect( blockEditorStore )\n\t\t);\n\t\tconst [ postContentClientId ] = getBlocksByName( 'core/post-content' );\n\t\treturn getClientIdsTree( postContentClientId ?? '' );\n\t}, [] );\n}\n"],
  "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAA0B;AAC1B,kBAAkB;AAElB,0BAA0C;AAE1C,yBAAuB;AAuBhB,SAAS,uBACf,OAC0B;AAC1B,MAAI,UAAwC;AAE5C,SAAQ,SAAU;AACjB,UAAM,SAAS,QAAQ;AAEvB,QACC,kBAAkB,cAAE,OACpB,OAAO,kBAAkB,cAAE,SAC3B,OAAO,IAAK,UAAW,MAAM,UAC7B,OAAO,IAAK,aAAc,aAAa,cAAE,OACxC;AACD,aAAO;AAAA,IACR;AAEA,cAAU,kBAAkB,cAAE,eAAe,SAAS;AAAA,EACvD;AAEA,SAAO;AACR;AAWO,SAAS,mBACf,OACgC;AAChC,QAAM,OAA+B,CAAC;AACtC,MAAI,UAA4B;AAEhC,SAAQ,SAAU;AACjB,UAAM,cAAc,QAAQ;AAE5B,QAAK,CAAE,eAAe,EAAI,uBAAuB,cAAE,QAAU;AAC5D,aAAO;AAAA,IACR;AAGA,QAAI,QAAQ;AACZ,aAAU,IAAI,GAAG,IAAI,YAAY,QAAQ,KAAM;AAC9C,UAAK,YAAY,IAAK,CAAE,MAAM,SAAU;AACvC,gBAAQ;AACR;AAAA,MACD;AAAA,IACD;AAEA,QAAK,UAAU,IAAK;AACnB,aAAO;AAAA,IACR;AAEA,SAAK,QAAS,KAAM;AAGpB,UAAM,cAAc,YAAY;AAChC,QACC,uBAAuB,cAAE,OACzB,YAAY,IAAK,UAAW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