// packages/block-editor/src/components/block-switcher/use-transformed-patterns.js
import { useMemo } from "@wordpress/element";
import { cloneBlock } from "@wordpress/blocks";
import { getMatchingBlockByName, getRetainedBlockAttributes } from "./utils";
var transformMatchingBlock = (match, selectedBlock) => {
  const retainedBlockAttributes = getRetainedBlockAttributes(
    selectedBlock.name,
    selectedBlock.attributes
  );
  match.attributes = {
    ...match.attributes,
    ...retainedBlockAttributes
  };
};
var getPatternTransformedBlocks = (selectedBlocks, patternBlocks) => {
  const _patternBlocks = patternBlocks.map(
    (block) => cloneBlock(block)
  );
  const consumedBlocks = /* @__PURE__ */ new Set();
  for (const selectedBlock of selectedBlocks) {
    let isMatch = false;
    for (const patternBlock of _patternBlocks) {
      const match = getMatchingBlockByName(
        patternBlock,
        selectedBlock.name,
        consumedBlocks
      );
      if (!match) {
        continue;
      }
      isMatch = true;
      consumedBlocks.add(match.clientId);
      transformMatchingBlock(match, selectedBlock);
      break;
    }
    if (!isMatch) {
      return;
    }
  }
  return _patternBlocks;
};
var useTransformedPatterns = (patterns, selectedBlocks) => {
  return useMemo(
    () => patterns.reduce((accumulator, _pattern) => {
      const transformedBlocks = getPatternTransformedBlocks(
        selectedBlocks,
        _pattern.blocks
      );
      if (transformedBlocks) {
        accumulator.push({
          ..._pattern,
          transformedBlocks
        });
      }
      return accumulator;
    }, []),
    [patterns, selectedBlocks]
  );
};
var use_transformed_patterns_default = useTransformedPatterns;
export {
  use_transformed_patterns_default as default,
  getPatternTransformedBlocks,
  transformMatchingBlock
};
//# sourceMappingURL=use-transformed-patterns.js.map
