"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/store/utils.js
var utils_exports = {};
__export(utils_exports, {
  checkAllowList: () => checkAllowList,
  checkAllowListRecursive: () => checkAllowListRecursive,
  getAllPatternsDependants: () => getAllPatternsDependants,
  getGrammar: () => getGrammar,
  getInsertBlockTypeDependants: () => getInsertBlockTypeDependants,
  getParsedPattern: () => getParsedPattern,
  isFiltered: () => isFiltered,
  mapUserPattern: () => mapUserPattern
});
module.exports = __toCommonJS(utils_exports);
var import_blocks = require("@wordpress/blocks");
var import_block_serialization_default_parser = require("@wordpress/block-serialization-default-parser");
var import_private_keys = require("./private-keys.cjs");
var import_lock_unlock = require("../lock-unlock.cjs");
var import_constants = require("./constants.cjs");
var import_private_selectors = require("./private-selectors.cjs");
var import_selectors = require("./selectors.cjs");
var import_utils = require("../components/inserter/block-patterns-tab/utils.cjs");
var isFiltered = /* @__PURE__ */ Symbol("isFiltered");
var parsedPatternCache = /* @__PURE__ */ new WeakMap();
var grammarMapCache = /* @__PURE__ */ new WeakMap();
function mapUserPattern(userPattern, __experimentalUserPatternCategories = []) {
  return {
    name: `core/block/${userPattern.id}`,
    id: userPattern.id,
    type: import_utils.INSERTER_PATTERN_TYPES.user,
    title: userPattern.title?.raw,
    categories: userPattern.wp_pattern_category?.map((catId) => {
      const category = __experimentalUserPatternCategories.find(
        ({ id }) => id === catId
      );
      return category ? category.slug : catId;
    }),
    content: userPattern.content?.raw,
    syncStatus: userPattern.wp_pattern_sync_status
  };
}
function parsePattern(pattern) {
  const blocks = (0, import_blocks.parse)(pattern.content, {
    __unstableSkipMigrationLogs: true
  });
  if (blocks.length === 1) {
    blocks[0].attributes = {
      ...blocks[0].attributes,
      metadata: {
        ...blocks[0].attributes.metadata || {},
        categories: pattern.categories,
        patternName: pattern.name,
        name: blocks[0].attributes.metadata?.name || pattern.title
      }
    };
  }
  return {
    ...pattern,
    blocks
  };
}
function getParsedPattern(pattern) {
  let parsedPattern = parsedPatternCache.get(pattern);
  if (!parsedPattern) {
    parsedPattern = parsePattern(pattern);
    parsedPatternCache.set(pattern, parsedPattern);
  }
  return parsedPattern;
}
function getGrammar(pattern) {
  let grammarMap = grammarMapCache.get(pattern);
  if (!grammarMap) {
    grammarMap = (0, import_block_serialization_default_parser.parse)(pattern.content);
    grammarMap = grammarMap.filter((block) => block.blockName !== null);
    grammarMapCache.set(pattern, grammarMap);
  }
  return grammarMap;
}
var checkAllowList = (list, item, defaultResult = null) => {
  if (typeof list === "boolean") {
    return list;
  }
  if (Array.isArray(list)) {
    if (list.includes("core/post-content") && item === null) {
      return true;
    }
    return list.includes(item);
  }
  return defaultResult;
};
var checkAllowListRecursive = (blocks, allowedBlockTypes) => {
  if (typeof allowedBlockTypes === "boolean") {
    return allowedBlockTypes;
  }
  const blocksQueue = [...blocks];
  while (blocksQueue.length > 0) {
    const block = blocksQueue.shift();
    const isAllowed = checkAllowList(
      allowedBlockTypes,
      block.name || block.blockName,
      true
    );
    if (!isAllowed) {
      return false;
    }
    block.innerBlocks?.forEach((innerBlock) => {
      blocksQueue.push(innerBlock);
    });
  }
  return true;
};
var getAllPatternsDependants = (select) => (state) => {
  return [
    state.settings.__experimentalBlockPatterns,
    state.settings[import_private_keys.userPatternCategoriesSelectKey]?.(select) ?? state.settings.__experimentalUserPatternCategories,
    state.settings.__experimentalReusableBlocks,
    state.settings[import_private_keys.selectBlockPatternsKey]?.(select),
    state.blockPatterns,
    (0, import_lock_unlock.unlock)(select(import_constants.STORE_NAME)).getReusableBlocks()
  ];
};
var getInsertBlockTypeDependants = () => (state, rootClientId) => {
  return [
    state.blockListSettings.get(rootClientId),
    state.blocks.byClientId.get(rootClientId),
    state.blocks.order.get(rootClientId || ""),
    state.settings.allowedBlockTypes,
    state.settings.templateLock,
    (0, import_selectors.getBlockEditingMode)(state, rootClientId),
    (0, import_private_selectors.getSectionRootClientId)(state),
    (0, import_private_selectors.isSectionBlock)(state, rootClientId),
    (0, import_private_selectors.getParentSectionBlock)(state, rootClientId)
  ];
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  checkAllowList,
  checkAllowListRecursive,
  getAllPatternsDependants,
  getGrammar,
  getInsertBlockTypeDependants,
  getParsedPattern,
  isFiltered,
  mapUserPattern
});
//# sourceMappingURL=utils.cjs.map
