"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/template-part/edit/selection-modal.js
var selection_modal_exports = {};
__export(selection_modal_exports, {
  default: () => TemplatePartSelectionModal
});
module.exports = __toCommonJS(selection_modal_exports);
var import_element = require("@wordpress/element");
var import_i18n = require("@wordpress/i18n");
var import_notices = require("@wordpress/notices");
var import_data = require("@wordpress/data");
var import_block_editor = require("@wordpress/block-editor");
var import_components = require("@wordpress/components");
var import_hooks = require("./utils/hooks");
var import_map_template_part_to_block_pattern = require("./utils/map-template-part-to-block-pattern");
var import_search_patterns = require("../../utils/search-patterns");
var import_jsx_runtime = require("react/jsx-runtime");
function TemplatePartSelectionModal({
  setAttributes,
  onClose,
  templatePartId = null,
  area,
  clientId
}) {
  const [searchValue, setSearchValue] = (0, import_element.useState)("");
  const { templateParts } = (0, import_hooks.useAlternativeTemplateParts)(
    area,
    templatePartId
  );
  const filteredTemplateParts = (0, import_element.useMemo)(() => {
    const partsAsPatterns = templateParts.map(
      (templatePart) => (0, import_map_template_part_to_block_pattern.mapTemplatePartToBlockPattern)(templatePart)
    );
    return (0, import_search_patterns.searchPatterns)(partsAsPatterns, searchValue);
  }, [templateParts, searchValue]);
  const blockPatterns = (0, import_hooks.useAlternativeBlockPatterns)(area, clientId);
  const filteredBlockPatterns = (0, import_element.useMemo)(() => {
    return (0, import_search_patterns.searchPatterns)(blockPatterns, searchValue);
  }, [blockPatterns, searchValue]);
  const { createSuccessNotice } = (0, import_data.useDispatch)(import_notices.store);
  const onTemplatePartSelect = (templatePart) => {
    setAttributes({
      slug: templatePart.slug,
      theme: templatePart.theme,
      area: void 0
    });
    createSuccessNotice(
      (0, import_i18n.sprintf)(
        /* translators: %s: template part title. */
        (0, import_i18n.__)('Template Part "%s" inserted.'),
        templatePart.title?.rendered || templatePart.slug
      ),
      {
        type: "snackbar"
      }
    );
    onClose();
  };
  const hasTemplateParts = !!filteredTemplateParts.length;
  const hasBlockPatterns = !!filteredBlockPatterns.length;
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "block-library-template-part__selection-content", children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-library-template-part__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")
      }
    ) }),
    hasTemplateParts && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h2", { children: (0, import_i18n.__)("Existing template parts") }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_block_editor.__experimentalBlockPatternsList,
        {
          blockPatterns: filteredTemplateParts,
          onClickPattern: (pattern) => {
            onTemplatePartSelect(pattern.templatePart);
          }
        }
      )
    ] }),
    !hasTemplateParts && !hasBlockPatterns && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.__experimentalHStack, { alignment: "center", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { children: (0, import_i18n.__)("No results found.") }) })
  ] });
}
//# sourceMappingURL=selection-modal.js.map
