"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/block-toolbar/change-design.js
var change_design_exports = {};
__export(change_design_exports, {
  default: () => ChangeDesign
});
module.exports = __toCommonJS(change_design_exports);
var import_components = require("@wordpress/components");
var import_i18n = require("@wordpress/i18n");
var import_blocks = require("@wordpress/blocks");
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_store = require("../../store");
var import_block_patterns_list = __toESM(require("../block-patterns-list"));
var import_jsx_runtime = require("react/jsx-runtime");
var EMPTY_ARRAY = [];
var MAX_PATTERNS_TO_SHOW = 6;
var POPOVER_PROPS = {
  placement: "bottom-start"
};
function ChangeDesign({ clientId }) {
  const { categories, currentPatternName, patterns } = (0, import_data.useSelect)(
    (select) => {
      const {
        getBlockAttributes,
        getBlockRootClientId,
        __experimentalGetAllowedPatterns
      } = select(import_store.store);
      const attributes = getBlockAttributes(clientId);
      const _categories = attributes?.metadata?.categories || EMPTY_ARRAY;
      const rootBlock = getBlockRootClientId(clientId);
      const _patterns = _categories.length > 0 ? __experimentalGetAllowedPatterns(rootBlock) : EMPTY_ARRAY;
      return {
        categories: _categories,
        currentPatternName: attributes?.metadata?.patternName,
        patterns: _patterns
      };
    },
    [clientId]
  );
  const { replaceBlocks } = (0, import_data.useDispatch)(import_store.store);
  const sameCategoryPatternsWithSingleWrapper = (0, import_element.useMemo)(() => {
    if (categories.length === 0 || !patterns || patterns.length === 0) {
      return EMPTY_ARRAY;
    }
    return patterns.filter((pattern) => {
      const isCorePattern = pattern.source === "core" || pattern.source?.startsWith("pattern-directory") && pattern.source !== "pattern-directory/theme";
      return (
        // Check if the pattern has only one top level block,
        // otherwise we may switch to a pattern that doesn't have replacement suggestions.
        pattern.blocks.length === 1 && // We exclude the core patterns and pattern directory patterns that are not theme patterns.
        !isCorePattern && // Exclude current pattern.
        currentPatternName !== pattern.name && pattern.categories?.some((category) => {
          return categories.includes(category);
        }) && // Check if the pattern is not a synced pattern.
        (pattern.syncStatus === "unsynced" || !pattern.id)
      );
    }).slice(0, MAX_PATTERNS_TO_SHOW);
  }, [categories, currentPatternName, patterns]);
  if (sameCategoryPatternsWithSingleWrapper.length < 2) {
    return null;
  }
  const onClickPattern = (pattern) => {
    const newBlocks = (pattern.blocks ?? []).map((block) => {
      return (0, import_blocks.cloneBlock)(block);
    });
    newBlocks[0].attributes.metadata = {
      ...newBlocks[0].attributes.metadata,
      categories
    };
    replaceBlocks(clientId, newBlocks);
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.Dropdown,
    {
      popoverProps: POPOVER_PROPS,
      renderToggle: ({ onToggle, isOpen }) => {
        return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.ToolbarGroup, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_components.ToolbarButton,
          {
            onClick: () => onToggle(!isOpen),
            "aria-expanded": isOpen,
            children: (0, import_i18n.__)("Change design")
          }
        ) });
      },
      renderContent: () => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_components.__experimentalDropdownContentWrapper,
        {
          className: "block-editor-block-toolbar-change-design-content-wrapper",
          paddingSize: "none",
          children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_block_patterns_list.default,
            {
              blockPatterns: sameCategoryPatternsWithSingleWrapper,
              onClickPattern,
              showTitlesAsTooltip: true
            }
          )
        }
      )
    }
  );
}
//# sourceMappingURL=change-design.js.map
