{"version":3,"file":"include-control.mjs","names":["coreStore"],"sources":["../../../../src/terms-query/edit/inspector-controls/include-control.jsx"],"sourcesContent":["import { FormTokenField } from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useState, useEffect, useMemo } from '@wordpress/element';\nimport { useDebounce } from '@wordpress/compose';\nimport { decodeEntities } from '@wordpress/html-entities';\n\nconst EMPTY_ARRAY = [];\nconst BASE_QUERY = {\n\torder: 'asc',\n\t_fields: 'id,name',\n\tcontext: 'view',\n};\n\nexport default function IncludeControl( {\n\tvalue: include,\n\ttaxonomy,\n\tonChange,\n\t...props\n} ) {\n\tconst [ search, setSearch ] = useState( '' );\n\tconst [ value, setValue ] = useState( EMPTY_ARRAY );\n\tconst [ suggestions, setSuggestions ] = useState( EMPTY_ARRAY );\n\tconst debouncedSearch = useDebounce( setSearch, 250 );\n\n\tconst { searchResults, searchHasResolved } = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! search ) {\n\t\t\t\treturn { searchResults: EMPTY_ARRAY, searchHasResolved: true };\n\t\t\t}\n\t\t\tconst { getEntityRecords, hasFinishedResolution } =\n\t\t\t\tselect( coreStore );\n\t\t\tconst selectorArgs = [\n\t\t\t\t'taxonomy',\n\t\t\t\ttaxonomy,\n\t\t\t\t{\n\t\t\t\t\t...BASE_QUERY,\n\t\t\t\t\tsearch,\n\t\t\t\t\torderby: 'name',\n\t\t\t\t\texclude: include,\n\t\t\t\t\tper_page: 20,\n\t\t\t\t},\n\t\t\t];\n\t\t\treturn {\n\t\t\t\tsearchResults: getEntityRecords( ...selectorArgs ),\n\t\t\t\tsearchHasResolved: hasFinishedResolution(\n\t\t\t\t\t'getEntityRecords',\n\t\t\t\t\tselectorArgs\n\t\t\t\t),\n\t\t\t};\n\t\t},\n\t\t[ search, taxonomy, include ]\n\t);\n\n\tconst currentTerms = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! include?.length ) {\n\t\t\t\treturn EMPTY_ARRAY;\n\t\t\t}\n\t\t\tconst { getEntityRecords } = select( coreStore );\n\t\t\treturn getEntityRecords( 'taxonomy', taxonomy, {\n\t\t\t\t...BASE_QUERY,\n\t\t\t\tinclude,\n\t\t\t\tper_page: include.length,\n\t\t\t} );\n\t\t},\n\t\t[ include, taxonomy ]\n\t);\n\n\t// Update the `value` state only after the selectors are resolved\n\t// to avoid emptying the input when we're changing terms.\n\tuseEffect( () => {\n\t\tif ( ! include?.length ) {\n\t\t\tsetValue( EMPTY_ARRAY );\n\t\t}\n\t\tif ( ! currentTerms?.length ) {\n\t\t\treturn;\n\t\t}\n\t\t// Returns only the existing entity ids. This prevents the component\n\t\t// from crashing in the editor, when non existing ids are provided.\n\t\tconst sanitizedValue = include.reduce( ( accumulator, id ) => {\n\t\t\tconst entity = currentTerms.find( ( term ) => term.id === id );\n\t\t\tif ( entity ) {\n\t\t\t\taccumulator.push( {\n\t\t\t\t\tid,\n\t\t\t\t\tvalue: decodeEntities( entity.name ),\n\t\t\t\t} );\n\t\t\t}\n\t\t\treturn accumulator;\n\t\t}, [] );\n\t\tsetValue( sanitizedValue );\n\t}, [ include, currentTerms ] );\n\n\tconst entitiesInfo = useMemo( () => {\n\t\tif ( ! searchResults?.length ) {\n\t\t\treturn { names: EMPTY_ARRAY, mapByName: {} };\n\t\t}\n\t\tconst names = [];\n\t\tconst mapByName = {};\n\t\tsearchResults.forEach( ( result ) => {\n\t\t\tconst decodedName = decodeEntities( result.name );\n\t\t\tnames.push( decodedName );\n\t\t\tmapByName[ decodedName ] = result;\n\t\t} );\n\t\treturn { names, mapByName };\n\t}, [ searchResults ] );\n\n\t// Update suggestions only when the query has resolved.\n\tuseEffect( () => {\n\t\tif ( ! searchHasResolved ) {\n\t\t\treturn;\n\t\t}\n\t\tsetSuggestions( entitiesInfo.names );\n\t}, [ entitiesInfo.names, searchHasResolved ] );\n\n\tconst getIdByValue = ( entitiesMappedByName, entity ) =>\n\t\tentity?.id || entitiesMappedByName?.[ entity ]?.id;\n\n\tconst onTermChange = ( newValue ) => {\n\t\tconst ids = Array.from(\n\t\t\tnewValue.reduce( ( accumulator, entity ) => {\n\t\t\t\t// Verify that new values point to existing entities.\n\t\t\t\tconst id = getIdByValue( entitiesInfo.mapByName, entity );\n\t\t\t\tif ( id ) {\n\t\t\t\t\taccumulator.add( id );\n\t\t\t\t}\n\t\t\t\treturn accumulator;\n\t\t\t}, new Set() )\n\t\t);\n\t\tsetSuggestions( EMPTY_ARRAY );\n\t\tonChange( ids );\n\t};\n\n\treturn (\n\t\t<FormTokenField\n\t\t\tvalue={ value }\n\t\t\tonInputChange={ debouncedSearch }\n\t\t\tsuggestions={ suggestions }\n\t\t\tonChange={ onTermChange }\n\t\t\thelp=\"\"\n\t\t\t{ ...props }\n\t\t/>\n\t);\n}\n"],"mappings":";;;;;;;;AAOA,MAAM,cAAc,CAAC;AACrB,MAAM,aAAa;CAClB,OAAO;CACP,SAAS;CACT,SAAS;AACV;AAEA,SAAwB,eAAgB,EACvC,OAAO,SACP,UACA,UACA,GAAG,SACA;CACH,MAAM,CAAE,QAAQ,aAAc,SAAU,EAAG;CAC3C,MAAM,CAAE,OAAO,YAAa,SAAU,WAAY;CAClD,MAAM,CAAE,aAAa,kBAAmB,SAAU,WAAY;CAC9D,MAAM,kBAAkB,YAAa,WAAW,GAAI;CAEpD,MAAM,EAAE,eAAe,sBAAsB,WAC1C,WAAY;EACb,IAAK,CAAE,QACN,OAAO;GAAE,eAAe;GAAa,mBAAmB;EAAK;EAE9D,MAAM,EAAE,kBAAkB,0BACzB,OAAQA,KAAU;EACnB,MAAM,eAAe;GACpB;GACA;GACA;IACC,GAAG;IACH;IACA,SAAS;IACT,SAAS;IACT,UAAU;GACX;EACD;EACA,OAAO;GACN,eAAe,iBAAkB,GAAG,YAAa;GACjD,mBAAmB,sBAClB,oBACA,YACD;EACD;CACD,GACA;EAAE;EAAQ;EAAU;CAAQ,CAC7B;CAEA,MAAM,eAAe,WAClB,WAAY;EACb,IAAK,CA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