{
  "version": 3,
  "sources": ["../../src/api/templates.js"],
  "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { renderToString } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { convertLegacyBlockNameAndAttributes } from './parser/convert-legacy-block';\nimport { createBlock } from './factory';\nimport { getBlockType } from './registration';\n\n/**\n * Checks whether a list of blocks matches a template by comparing the block names.\n *\n * @param {Array} blocks   Block list.\n * @param {Array} template Block template.\n *\n * @return {boolean} Whether the list of blocks matches a templates.\n */\nexport function doBlocksMatchTemplate( blocks = [], template = [] ) {\n\treturn (\n\t\tblocks.length === template.length &&\n\t\ttemplate.every( ( [ name, , innerBlocksTemplate ], index ) => {\n\t\t\tconst block = blocks[ index ];\n\t\t\treturn (\n\t\t\t\tname === block.name &&\n\t\t\t\tdoBlocksMatchTemplate( block.innerBlocks, innerBlocksTemplate )\n\t\t\t);\n\t\t} )\n\t);\n}\n\nconst isHTMLAttribute = ( attributeDefinition ) =>\n\tattributeDefinition?.source === 'html';\n\nconst isQueryAttribute = ( attributeDefinition ) =>\n\tattributeDefinition?.source === 'query';\n\nfunction normalizeAttributes( schema, values ) {\n\tif ( ! values ) {\n\t\treturn {};\n\t}\n\n\treturn Object.fromEntries(\n\t\tObject.entries( values ).map( ( [ key, value ] ) => [\n\t\t\tkey,\n\t\t\tnormalizeAttribute( schema[ key ], value ),\n\t\t] )\n\t);\n}\n\nfunction normalizeAttribute( definition, value ) {\n\tif ( isHTMLAttribute( definition ) && Array.isArray( value ) ) {\n\t\t// Introduce a deprecated call at this point\n\t\t// When we're confident that \"children\" format should be removed from the templates.\n\n\t\treturn renderToString( value );\n\t}\n\n\tif ( isQueryAttribute( definition ) && value ) {\n\t\treturn value.map( ( subValues ) => {\n\t\t\treturn normalizeAttributes( definition.query, subValues );\n\t\t} );\n\t}\n\n\treturn value;\n}\n\n/**\n * Synchronize a block list with a block template.\n *\n * Synchronizing a block list with a block template means that we loop over the blocks\n * keep the block as is if it matches the block at the same position in the template\n * (If it has the same name) and if doesn't match, we create a new block based on the template.\n * Extra blocks not present in the template are removed.\n *\n * @param {Array} blocks   Block list.\n * @param {Array} template Block template.\n *\n * @return {Array} Updated Block list.\n */\nexport function synchronizeBlocksWithTemplate( blocks = [], template ) {\n\t// If no template is provided, return blocks unmodified.\n\tif ( ! template ) {\n\t\treturn blocks;\n\t}\n\n\treturn template.map(\n\t\t( [ name, attributes, innerBlocksTemplate ], index ) => {\n\t\t\tconst block = blocks[ index ];\n\n\t\t\tif ( block && block.name === name ) {\n\t\t\t\tconst innerBlocks = synchronizeBlocksWithTemplate(\n\t\t\t\t\tblock.innerBlocks,\n\t\t\t\t\tinnerBlocksTemplate\n\t\t\t\t);\n\t\t\t\treturn { ...block, innerBlocks };\n\t\t\t}\n\n\t\t\t// To support old templates that were using the \"children\" format\n\t\t\t// for the attributes using \"html\" strings now, we normalize the template attributes\n\t\t\t// before creating the blocks.\n\n\t\t\tconst blockType = getBlockType( name );\n\n\t\t\tconst normalizedAttributes = normalizeAttributes(\n\t\t\t\tblockType?.attributes ?? {},\n\t\t\t\tattributes\n\t\t\t);\n\n\t\t\tconst [ blockName, blockAttributes ] =\n\t\t\t\tconvertLegacyBlockNameAndAttributes(\n\t\t\t\t\tname,\n\t\t\t\t\tnormalizedAttributes\n\t\t\t\t);\n\n\t\t\treturn createBlock(\n\t\t\t\tblockName,\n\t\t\t\tblockAttributes,\n\t\t\t\tsynchronizeBlocksWithTemplate( [], innerBlocksTemplate )\n\t\t\t);\n\t\t}\n\t);\n}\n"],
  "mappings": ";AAGA,SAAS,sBAAsB;AAK/B,SAAS,2CAA2C;AACpD,SAAS,mBAAmB;AAC5B,SAAS,oBAAoB;AAUtB,SAAS,sBAAuB,SAAS,CAAC,GAAG,WAAW,CAAC,GAAI;AACnE,SACC,OAAO,WAAW,SAAS,UAC3B,SAAS,MAAO,CAAE,CAAE,MAAM,EAAE,mBAAoB,GAAG,UAAW;AAC7D,UAAM,QAAQ,OAAQ,KAAM;AAC5B,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