{"version":3,"file":"private-hooks.cjs","names":["useDispatch","coreStore","useSelect","useMemo","CATEGORY_SLUG"],"sources":["../src/private-hooks.ts"],"sourcesContent":["import { useSelect, useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { Term } from '@wordpress/core-data';\nimport { useMemo } from '@wordpress/element';\nimport { CATEGORY_SLUG } from './components/category-selector';\nimport type {\n\tPatternCategoryEntry,\n\tPatternCategoryMap,\n\tTermExistsError,\n} from './types';\n\n/**\n * Helper hook that creates a Map with the core and user patterns categories\n * and removes any duplicates. It's used when we need to create new user\n * categories when creating or importing patterns.\n * This hook also provides a function to find or create a pattern category.\n *\n * @return The merged categories map and the callback function to find or create a category.\n */\nexport function useAddPatternCategory(): {\n\tcategoryMap: PatternCategoryMap;\n\tfindOrCreateTerm: ( term: string ) => Promise< number >;\n} {\n\tconst { saveEntityRecord, invalidateResolution } = useDispatch( coreStore );\n\tconst { corePatternCategories, userPatternCategories } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getUserPatternCategories, getBlockPatternCategories } =\n\t\t\t\tselect( coreStore );\n\n\t\t\treturn {\n\t\t\t\tcorePatternCategories: getBlockPatternCategories(),\n\t\t\t\tuserPatternCategories: getUserPatternCategories(),\n\t\t\t};\n\t\t},\n\t\t[]\n\t);\n\tconst categoryMap = useMemo( () => {\n\t\t// Merge the user and core pattern categories and remove any duplicates.\n\t\tconst uniqueCategories = new Map< string, PatternCategoryEntry >();\n\t\tuserPatternCategories.forEach( ( category ) => {\n\t\t\tuniqueCategories.set( category.label.toLowerCase(), {\n\t\t\t\tlabel: category.label,\n\t\t\t\tname: category.name,\n\t\t\t\tid: category.id,\n\t\t\t} );\n\t\t} );\n\n\t\tcorePatternCategories.forEach( ( category ) => {\n\t\t\tif (\n\t\t\t\t! uniqueCategories.has( category.label.toLowerCase() ) &&\n\t\t\t\t// There are two core categories with `Post` label so explicitly remove the one with\n\t\t\t\t// the `query` slug to avoid any confusion.\n\t\t\t\tcategory.name !== 'query'\n\t\t\t) {\n\t\t\t\tuniqueCategories.set( category.label.toLowerCase(), {\n\t\t\t\t\tlabel: category.label,\n\t\t\t\t\tname: category.name,\n\t\t\t\t} );\n\t\t\t}\n\t\t} );\n\t\treturn uniqueCategories;\n\t}, [ userPatternCategories, corePatternCategories ] );\n\n\tasync function findOrCreateTerm( term: string ): Promise< number > {\n\t\ttry {\n\t\t\tconst existingTerm = categoryMap.get( term.toLowerCase() );\n\t\t\tif ( existingTerm?.id ) {\n\t\t\t\treturn existingTerm.id;\n\t\t\t}\n\t\t\t// If we have an existing core category we need to match the new user category to the\n\t\t\t// correct slug rather than autogenerating it to prevent duplicates, eg. the core `Headers`\n\t\t\t// category uses the singular `header` as the slug.\n\t\t\tconst termData = existingTerm\n\t\t\t\t? { name: existingTerm.label, slug: existingTerm.name }\n\t\t\t\t: { name: term };\n\t\t\tconst newTerm = ( await saveEntityRecord(\n\t\t\t\t'taxonomy',\n\t\t\t\tCATEGORY_SLUG,\n\t\t\t\ttermData,\n\t\t\t\t{ throwOnError: true }\n\t\t\t) ) as Term< 'edit' >;\n\t\t\tinvalidateResolution( 'getUserPatternCategories' );\n\t\t\treturn newTerm.id;\n\t\t} catch ( error ) {\n\t\t\t// A REST API error is a plain object, not an `Error`.\n\t\t\tif ( ( error as TermExistsError ).code !== 'term_exists' ) {\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t\t// Checked above to be the error for an existing term.\n\t\t\treturn ( error as TermExistsError ).data.term_id;\n\t\t}\n\t}\n\n\treturn { categoryMap, findOrCreateTerm };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmBA,SAAgB,wBAGd;CACD,MAAM,EAAE,kBAAkB,0BAAA,GAAyBA,gBAAAA,YAAAA,CAAaC,qBAAAA,KAAU;CAC1E,MAAM,EAAE,uBAAuB,2BAAA,GAA0BC,gBAAAA,UAAAA,EACtD,WAAY;EACb,MAAM,EAAE,0BAA0B,8BACjC,OAAQD,qBAAAA,KAAU;EAEnB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