"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-library/src/query/edit/pattern-selection.js
var pattern_selection_exports = {};
__export(pattern_selection_exports, {
  PatternSelectionModal: () => PatternSelectionModal,
  default: () => PatternSelection,
  useBlockPatterns: () => useBlockPatterns
});
module.exports = __toCommonJS(pattern_selection_exports);
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_components = require("@wordpress/components");
var import_block_editor = require("@wordpress/block-editor");
var import_i18n = require("@wordpress/i18n");
var import_utils = require("../utils");
var import_search_patterns = require("../../utils/search-patterns");
var import_jsx_runtime = require("react/jsx-runtime");
function PatternSelectionModal({
  clientId,
  attributes,
  setIsPatternSelectionModalOpen
}) {
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.Modal,
    {
      overlayClassName: "block-library-query-pattern__selection-modal",
      title: (0, import_i18n.__)("Choose a pattern"),
      onRequestClose: () => setIsPatternSelectionModalOpen(false),
      isFullScreen: true,
      children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PatternSelection, { clientId, attributes })
    }
  );
}
function useBlockPatterns(clientId, attributes) {
  const blockNameForPatterns = (0, import_utils.useBlockNameForPatterns)(
    clientId,
    attributes
  );
  const allPatterns = (0, import_utils.usePatterns)(clientId, blockNameForPatterns);
  const rootBlockPatterns = (0, import_element.useMemo)(() => {
    return allPatterns.filter((pattern) => {
      return pattern.blocks?.[0]?.name === blockNameForPatterns;
    });
  }, [allPatterns, blockNameForPatterns]);
  return rootBlockPatterns;
}
function PatternSelection({
  clientId,
  attributes,
  showTitlesAsTooltip = false,
  showSearch = true
}) {
  const [searchValue, setSearchValue] = (0, import_element.useState)("");
  const { replaceBlock, selectBlock } = (0, import_data.useDispatch)(import_block_editor.store);
  const blockPatterns = useBlockPatterns(clientId, attributes);
  const blockPreviewContext = (0, import_element.useMemo)(
    () => ({
      previewPostType: attributes.query.postType
    }),
    [attributes.query.postType]
  );
  const filteredBlockPatterns = (0, import_element.useMemo)(() => {
    return (0, import_search_patterns.searchPatterns)(blockPatterns, searchValue);
  }, [blockPatterns, searchValue]);
  const onBlockPatternSelect = (pattern, blocks) => {
    const { newBlocks, queryClientIds } = (0, import_utils.getTransformedBlocksFromPattern)(
      blocks,
      attributes
    );
    replaceBlock(clientId, newBlocks);
    if (queryClientIds[0]) {
      selectBlock(queryClientIds[0]);
    }
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "block-library-query-pattern__selection-content", children: [
    showSearch && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-library-query-pattern__selection-search", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_components.SearchControl,
      {
        __nextHasNoMarginBottom: true,
        onChange: setSearchValue,
        value: searchValue,
        label: (0, import_i18n.__)("Search"),
        placeholder: (0, import_i18n.__)("Search")
      }
    ) }),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_editor.BlockContextProvider, { value: blockPreviewContext, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_block_editor.__experimentalBlockPatternsList,
      {
        blockPatterns: filteredBlockPatterns,
        onClickPattern: onBlockPatternSelect,
        showTitlesAsTooltip
      }
    ) })
  ] });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PatternSelectionModal,
  useBlockPatterns
});
//# sourceMappingURL=pattern-selection.js.map
