"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/block-switcher/use-transformed-patterns.js
var use_transformed_patterns_exports = {};
__export(use_transformed_patterns_exports, {
  default: () => use_transformed_patterns_default,
  getPatternTransformedBlocks: () => getPatternTransformedBlocks,
  transformMatchingBlock: () => transformMatchingBlock
});
module.exports = __toCommonJS(use_transformed_patterns_exports);
var import_element = require("@wordpress/element");
var import_blocks = require("@wordpress/blocks");
var import_utils = require("./utils");
var transformMatchingBlock = (match, selectedBlock) => {
  const retainedBlockAttributes = (0, import_utils.getRetainedBlockAttributes)(
    selectedBlock.name,
    selectedBlock.attributes
  );
  match.attributes = {
    ...match.attributes,
    ...retainedBlockAttributes
  };
};
var getPatternTransformedBlocks = (selectedBlocks, patternBlocks) => {
  const _patternBlocks = patternBlocks.map(
    (block) => (0, import_blocks.cloneBlock)(block)
  );
  const consumedBlocks = /* @__PURE__ */ new Set();
  for (const selectedBlock of selectedBlocks) {
    let isMatch = false;
    for (const patternBlock of _patternBlocks) {
      const match = (0, import_utils.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 (0, import_element.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;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  getPatternTransformedBlocks,
  transformMatchingBlock
});
//# sourceMappingURL=use-transformed-patterns.js.map
