{"version":3,"file":"utils.cjs","names":["useSelect","coreStore","cleanForSlug"],"sources":["../../../src/components/create-template-part-modal/utils.ts"],"sourcesContent":["import { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { WpTemplatePart } from '@wordpress/core-data';\nimport { cleanForSlug } from '@wordpress/url';\n\nconst EMPTY_ARRAY: WpTemplatePart[] = [];\n\nexport function useExistingTemplateParts(): WpTemplatePart[] {\n\treturn useSelect(\n\t\t( select ) =>\n\t\t\tselect( coreStore ).getEntityRecords< WpTemplatePart >(\n\t\t\t\t'postType',\n\t\t\t\t'wp_template_part',\n\t\t\t\t{ per_page: -1 }\n\t\t\t) ?? EMPTY_ARRAY,\n\t\t[]\n\t);\n}\n\n/**\n * Return a unique template part title based on\n * the given title and existing template parts.\n *\n * @param title         The original template part title.\n * @param templateParts The array of template part entities.\n * @return A unique template part title.\n */\nexport function getUniqueTemplatePartTitle(\n\ttitle: string,\n\ttemplateParts: WpTemplatePart[]\n): string {\n\tconst lowercaseTitle = title.toLowerCase();\n\tconst existingTitles = templateParts.map( ( templatePart ) =>\n\t\ttemplatePart.title.rendered.toLowerCase()\n\t);\n\n\tif ( ! existingTitles.includes( lowercaseTitle ) ) {\n\t\treturn title;\n\t}\n\n\tlet suffix = 2;\n\twhile ( existingTitles.includes( `${ lowercaseTitle } ${ suffix }` ) ) {\n\t\tsuffix++;\n\t}\n\n\treturn `${ title } ${ suffix }`;\n}\n\n/**\n * Get a valid slug for a template part.\n * Currently template parts only allow latin chars.\n * The fallback slug will receive suffix by default.\n *\n * @param title The template part title.\n * @return A valid template part slug.\n */\nexport function getCleanTemplatePartSlug( title: string ): string {\n\treturn cleanForSlug( title ).replace( /[^\\w-]+/g, '' ) || 'wp-custom-part';\n}\n"],"mappings":";;;;;;;;;AAKA,MAAM,cAAgC,CAAC;AAEvC,SAAgB,2BAA6C;CAC5D,QAAA,GAAOA,gBAAAA,UAAAA,EACJ,WACD,OAAQC,qBAAAA,KAAU,CAAC,CAAC,iBACnB,YACA,oBACA,EAAE,UAAU,GAAG,CAChB,KAAK,aACN,CAAC,CACF;AACD;;;;;;;;;AAUA,SAAgB,2BACf,OACA,eACS;CACT,MAAM,iBAAiB,MAAM,YAAY;CACzC,MAAM,iBAAiB,cAAc,KAAO,iBAC3C,aAAa,MAAM,SAAS,YAAY,CACzC;CAEA,IAAK,CAAE,eAAe,SAAU,cAAe,GAC9C,OAAO;CAGR,IAAI,SAAS;CACb,OAAQ,eAAe,SAAU,GAAI,eAAgB,GAAI,QAAU,GAClE;CAGD,OAAO,GAAI,MAAO,GAAI;AACvB;;;;;;;;;AAUA,SAAgB,yBAA0B,OAAwB;CACjE,QAAA,GAAOC,eAAAA,aAAAA,CAAc,KAAM,CAAC,CAAC,QAAS,YAAY,EAAG,KAAK;AAC3D"}