{
  "version": 3,
  "sources": ["../../../src/components/editor/use-resolve-edited-entity.js"],
  "sourcesContent": ["import { useEffect, useMemo, useRef } from '@wordpress/element';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { store as coreDataStore } from '@wordpress/core-data';\nimport { privateApis as routerPrivateApis } from '@wordpress/router';\nimport { store as editSiteStore } from '../../store';\nimport { unlock } from '../../lock-unlock';\nimport {\n\tATTACHMENT_POST_TYPE,\n\tTEMPLATE_POST_TYPE,\n\tTEMPLATE_PART_POST_TYPE,\n\tNAVIGATION_POST_TYPE,\n\tPATTERN_TYPES,\n} from '../../utils/constants';\n\nconst { useLocation } = unlock( routerPrivateApis );\n\nconst postTypesWithoutParentTemplate = [\n\tATTACHMENT_POST_TYPE,\n\tTEMPLATE_POST_TYPE,\n\tTEMPLATE_PART_POST_TYPE,\n\tNAVIGATION_POST_TYPE,\n\tPATTERN_TYPES.user,\n];\n\nconst authorizedPostTypes = [ 'page', 'post' ];\n\nfunction getPostType( name ) {\n\tlet postType;\n\tif ( name === 'navigation-item' ) {\n\t\tpostType = NAVIGATION_POST_TYPE;\n\t} else if ( name === 'pattern-item' ) {\n\t\tpostType = PATTERN_TYPES.user;\n\t} else if ( name === 'template-part-item' ) {\n\t\tpostType = TEMPLATE_PART_POST_TYPE;\n\t} else if ( name === 'templates' ) {\n\t\tpostType = TEMPLATE_POST_TYPE;\n\t} else if ( name === 'template-item' ) {\n\t\tpostType = TEMPLATE_POST_TYPE;\n\t} else if ( name === 'page-item' || name === 'pages' ) {\n\t\tpostType = 'page';\n\t} else if ( name === 'post-item' || name === 'posts' ) {\n\t\tpostType = 'post';\n\t}\n\n\treturn postType;\n}\n\nexport function useResolveEditedEntity() {\n\tconst { editEntityRecord } = useDispatch( coreDataStore );\n\tconst { hasEntityRecord } = useSelect( coreDataStore );\n\tconst { name, params = {}, query } = useLocation();\n\tconst { postId = query?.postId } = params; // Fallback to query param for postId for list view routes.\n\tconst postType = getPostType( name, postId ) ?? query?.postType;\n\t// Extract selectedBlock from URL for selection restoration on navigation back.\n\tconst { selectedBlock } = query;\n\n\t// Track which selection we've applied to avoid re-applying the same one,\n\t// but allow applying a new one if the URL changes.\n\tconst appliedSelectionRef = useRef( null );\n\n\tconst homePage = useSelect( ( select ) => {\n\t\tconst { getHomePage } = unlock( select( coreDataStore ) );\n\t\treturn getHomePage();\n\t}, [] );\n\n\t/**\n\t * This is a hook that recreates the logic to resolve a template for a given WordPress postID postTypeId\n\t * in order to match the frontend as closely as possible in the site editor.\n\t *\n\t * It is not possible to rely on the server logic because there maybe unsaved changes that impact the template resolution.\n\t */\n\tconst resolvedTemplateId = useSelect(\n\t\t( select ) => {\n\t\t\t// If we're rendering a post type that doesn't have a template\n\t\t\t// no need to resolve its template.\n\t\t\tif (\n\t\t\t\tpostTypesWithoutParentTemplate.includes( postType ) &&\n\t\t\t\tpostId\n\t\t\t) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Don't trigger resolution for multi-selected posts.\n\t\t\tif ( postId && postId.includes( ',' ) ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst { getTemplateId } = unlock( select( coreDataStore ) );\n\n\t\t\t// If we're rendering a specific page, we need to resolve its template.\n\t\t\t// The site editor only supports pages for now, not other CPTs.\n\t\t\tif (\n\t\t\t\tpostType &&\n\t\t\t\tpostId &&\n\t\t\t\tauthorizedPostTypes.includes( postType )\n\t\t\t) {\n\t\t\t\treturn getTemplateId( postType, postId );\n\t\t\t}\n\n\t\t\t// If we're rendering the home page, and we have a static home page, resolve its template.\n\t\t\tif ( homePage?.postType === 'page' ) {\n\t\t\t\treturn getTemplateId( 'page', homePage?.postId );\n\t\t\t}\n\n\t\t\tif ( homePage?.postType === 'wp_template' ) {\n\t\t\t\treturn homePage?.postId;\n\t\t\t}\n\t\t},\n\t\t[ homePage, postId, postType ]\n\t);\n\n\tconst context = useMemo( () => {\n\t\tif ( postTypesWithoutParentTemplate.includes( postType ) && postId ) {\n\t\t\treturn {};\n\t\t}\n\n\t\tif ( postType && postId && authorizedPostTypes.includes( postType ) ) {\n\t\t\treturn { postType, postId };\n\t\t}\n\t\t// TODO: for post types lists we should probably not render the front page, but maybe a placeholder\n\t\t// with a message like \"Select a page\" or something similar.\n\t\tif ( homePage?.postType === 'page' ) {\n\t\t\treturn { postType: 'page', postId: homePage?.postId };\n\t\t}\n\n\t\treturn {};\n\t}, [ homePage, postType, postId ] );\n\n\t// Compute entity info based on conditions\n\tlet entity;\n\tif ( postTypesWithoutParentTemplate.includes( postType ) && postId ) {\n\t\tentity = { isReady: true, postType, postId, context };\n\t} else if ( !! homePage ) {\n\t\tentity = {\n\t\t\tisReady: resolvedTemplateId !== undefined,\n\t\t\tpostType: TEMPLATE_POST_TYPE,\n\t\t\tpostId: resolvedTemplateId,\n\t\t\tcontext,\n\t\t};\n\t} else {\n\t\tentity = { isReady: false };\n\t}\n\n\t// Restore selection from URL synchronously, before EditorProvider renders.\n\t// This ensures the selection is available when blocks are reset.\n\t// When editing a page with a template, EditorProvider reads selection from\n\t// the page entity (context), not the template entity.\n\tif (\n\t\tselectedBlock &&\n\t\tentity.isReady &&\n\t\tappliedSelectionRef.current !== selectedBlock\n\t) {\n\t\tconst selectionPostType = entity.context?.postId\n\t\t\t? entity.context.postType\n\t\t\t: entity.postType;\n\t\tconst selectionPostId = entity.context?.postId\n\t\t\t? entity.context.postId\n\t\t\t: entity.postId;\n\n\t\t// Only apply selection if the entity record is loaded,\n\t\t// otherwise editEntityRecord will throw.\n\t\tif (\n\t\t\thasEntityRecord( 'postType', selectionPostType, selectionPostId )\n\t\t) {\n\t\t\teditEntityRecord(\n\t\t\t\t'postType',\n\t\t\t\tselectionPostType,\n\t\t\t\tselectionPostId,\n\t\t\t\t{\n\t\t\t\t\tselection: {\n\t\t\t\t\t\tselectionStart: { clientId: selectedBlock },\n\t\t\t\t\t\tselectionEnd: { clientId: selectedBlock },\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{ undoIgnore: true }\n\t\t\t);\n\t\t\tappliedSelectionRef.current = selectedBlock;\n\t\t}\n\t}\n\n\treturn entity;\n}\n\nexport function useSyncDeprecatedEntityIntoState( {\n\tpostType,\n\tpostId,\n\tcontext,\n\tisReady,\n} ) {\n\tconst { setEditedEntity } = useDispatch( editSiteStore );\n\n\tuseEffect( () => {\n\t\tif ( isReady ) {\n\t\t\t// setEditedEntity expects a string (because the postId used to be\n\t\t\t// the template slug, even for edited templates). Now the postId can\n\t\t\t// be a number (either because it's an auto-draft or edited\n\t\t\t// template). Passing a number could break plugins doing things like\n\t\t\t// `id.includes`. It would be way more complex to keep passing the\n\t\t\t// template slug, while also being incorrect, so the easiest\n\t\t\t// solution is to cast the postId to a string.\n\t\t\tsetEditedEntity( postType, String( postId ), context );\n\t\t}\n\t}, [ isReady, postType, postId, context, setEditedEntity ] );\n}\n"],
  "mappings": ";AAAA,SAAS,WAAW,SAAS,cAAc;AAC3C,SAAS,WAAW,mBAAmB;AACvC,SAAS,SAAS,qBAAqB;AACvC,SAAS,eAAe,yBAAyB;AACjD,SAAS,SAAS,qBAAqB;AACvC,SAAS,cAAc;AACvB;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAEP,IAAM,EAAE,YAAY,IAAI,OAAQ,iBAAkB;AAElD,IAAM,iCAAiC;AAAA,EACtC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,cAAc;AACf;AAEA,IAAM,sBAAsB,CAAE,QAAQ,MAAO;AAE7C,SAAS,YAAa,MAAO;AAC5B,MAAI;AACJ,MAAK,SAAS,mBAAoB;AACjC,eAAW;AAAA,EACZ,WAAY,SAAS,gBAAiB;AACrC,eAAW,cAAc;AAAA,EAC1B,WAAY,SAAS,sBAAuB;AAC3C,eAAW;AAAA,EACZ,WAAY,SAAS,aAAc;AAClC,eAAW;AAAA,EACZ,WAAY,SAAS,iBAAkB;AACtC,eAAW;AAAA,EACZ,WAAY,SAAS,eAAe,SAAS,SA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