{"version":3,"file":"use-term-count.cjs","names":["useEntityProp","useSelect","coreStore"],"sources":["../../src/term-count/use-term-count.js"],"sourcesContent":["import { store as coreStore, useEntityProp } from '@wordpress/core-data';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Hook to fetch term count based on context or fallback to template parsing.\n *\n * This hook prioritizes context-provided termId and taxonomy, but falls back to\n * template-based detection when no context is available.\n *\n * @param {string|number} termId   The term ID from context\n * @param {string}        taxonomy The taxonomy name from context\n */\nexport function useTermCount( termId, taxonomy ) {\n\tconst [ count ] = useEntityProp( 'taxonomy', taxonomy, 'count', termId );\n\n\t// Fallback approach: Parse template slug when no context is available.\n\tconst templateBasedData = useTemplateBasedTermData();\n\n\tconst hasContext = Boolean( termId && taxonomy );\n\n\treturn {\n\t\thasContext,\n\t\ttermCount: hasContext ? count || '' : templateBasedData,\n\t};\n}\n\n/**\n * Fallback hook to fetch term data from template context (backward compatibility).\n * This maintains the same logic as the original implementation for cases where\n * no termId/taxonomy context is provided.\n */\nfunction useTemplateBasedTermData() {\n\tconst templateSlug = useSelect( ( select ) => {\n\t\t// Access core/editor by string to avoid @wordpress/editor dependency.\n\t\t// eslint-disable-next-line @wordpress/data-no-store-string-literals\n\t\tconst { getCurrentPostId, getCurrentPostType, getCurrentTemplateId } =\n\t\t\tselect( 'core/editor' );\n\t\tconst currentPostType = getCurrentPostType();\n\t\tconst templateId =\n\t\t\tgetCurrentTemplateId() ||\n\t\t\t( currentPostType === 'wp_template' ? getCurrentPostId() : null );\n\n\t\treturn templateId\n\t\t\t? select( coreStore ).getEditedEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_template',\n\t\t\t\t\ttemplateId\n\t\t\t\t)?.slug\n\t\t\t: null;\n\t}, [] );\n\n\tconst taxonomyMatches = templateSlug?.match(\n\t\t/^(category|tag|taxonomy-([^-]+))$|^(((category|tag)|taxonomy-([^-]+))-(.+))$/\n\t);\n\n\tlet taxonomy;\n\tlet termSlug;\n\n\tif ( taxonomyMatches ) {\n\t\t// If it's for all taxonomies of a type (e.g., category, tag).\n\t\tif ( taxonomyMatches[ 1 ] ) {\n\t\t\ttaxonomy = taxonomyMatches[ 2 ]\n\t\t\t\t? taxonomyMatches[ 2 ]\n\t\t\t\t: taxonomyMatches[ 1 ];\n\t\t}\n\t\t// If it's for a specific term (e.g., category-news, tag-featured).\n\t\telse if ( taxonomyMatches[ 3 ] ) {\n\t\t\ttaxonomy = taxonomyMatches[ 6 ]\n\t\t\t\t? taxonomyMatches[ 6 ]\n\t\t\t\t: taxonomyMatches[ 4 ];\n\t\t\ttermSlug = taxonomyMatches[ 7 ];\n\t\t}\n\n\t\ttaxonomy = taxonomy === 'tag' ? 'post_tag' : taxonomy;\n\t}\n\n\treturn useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! taxonomy || ! termSlug ) {\n\t\t\t\treturn '';\n\t\t\t}\n\n\t\t\tconst { getEntityRecords } = select( coreStore );\n\n\t\t\tconst termRecords = getEntityRecords( 'taxonomy', taxonomy, {\n\t\t\t\tslug: termSlug,\n\t\t\t\tper_page: 1,\n\t\t\t} );\n\n\t\t\tif ( termRecords && termRecords[ 0 ] ) {\n\t\t\t\treturn termRecords[ 0 ].count || '';\n\t\t\t}\n\n\t\t\treturn '';\n\t\t},\n\t\t[ taxonomy, termSlug ]\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAYA,SAAgB,aAAc,QAAQ,UAAW;CAChD,MAAM,CAAE,UAAA,GAAUA,qBAAAA,cAAAA,CAAe,YAAY,UAAU,SAAS,MAAO;CAGvE,MAAM,oBAAoB,yBAAyB;CAEnD,MAAM,aAAa,QAAS,UAAU,QAAS;CAE/C,OAAO;EACN;EACA,WAAW,aAAa,SAAS,KAAK;CACvC;AACD;;;;;;AAOA,SAAS,2BAA2B;CAoBnC,MAAM,mBAAA,GAnBeC,gBAAAA,UAAAA,EAAa,WAAY;EAG7C,MAAM,EAAE,kBAAkB,oBAAoB,yBAC7C,OAAQ,aAAc;EACv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