"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/utils/hooks.js
var hooks_exports = {};
__export(hooks_exports, {
  useAlternativeBlockPatterns: () => useAlternativeBlockPatterns,
  useAlternativeTemplateParts: () => useAlternativeTemplateParts,
  useCreateTemplatePartFromBlocks: () => useCreateTemplatePartFromBlocks,
  useTemplatePartArea: () => useTemplatePartArea
});
module.exports = __toCommonJS(hooks_exports);
var import_change_case = require("change-case");
var import_data = require("@wordpress/data");
var import_core_data = require("@wordpress/core-data");
var import_block_editor = require("@wordpress/block-editor");
var import_element = require("@wordpress/element");
var import_blocks = require("@wordpress/blocks");
var import_i18n = require("@wordpress/i18n");
var import_create_template_part_id = require("./create-template-part-id");
function useAlternativeTemplateParts(area, excludedId) {
  const { templateParts, isResolving } = (0, import_data.useSelect)((select) => {
    const { getEntityRecords, isResolving: _isResolving } = select(import_core_data.store);
    const query = { per_page: -1 };
    return {
      templateParts: getEntityRecords(
        "postType",
        "wp_template_part",
        query
      ),
      isResolving: _isResolving("getEntityRecords", [
        "postType",
        "wp_template_part",
        query
      ])
    };
  }, []);
  const filteredTemplateParts = (0, import_element.useMemo)(() => {
    if (!templateParts) {
      return [];
    }
    return templateParts.filter(
      (templatePart) => (0, import_create_template_part_id.createTemplatePartId)(
        templatePart.theme,
        templatePart.slug
      ) !== excludedId && (!area || "uncategorized" === area || templatePart.area === area)
    ) || [];
  }, [templateParts, area, excludedId]);
  return {
    templateParts: filteredTemplateParts,
    isResolving
  };
}
function useAlternativeBlockPatterns(area, clientId) {
  return (0, import_data.useSelect)(
    (select) => {
      const blockNameWithArea = area ? `core/template-part/${area}` : "core/template-part";
      const { getBlockRootClientId, getPatternsByBlockTypes } = select(import_block_editor.store);
      const rootClientId = getBlockRootClientId(clientId);
      return getPatternsByBlockTypes(blockNameWithArea, rootClientId);
    },
    [area, clientId]
  );
}
function useCreateTemplatePartFromBlocks(area, setAttributes) {
  const { saveEntityRecord } = (0, import_data.useDispatch)(import_core_data.store);
  return async (blocks = [], title = (0, import_i18n.__)("Untitled Template Part")) => {
    const cleanSlug = (0, import_change_case.paramCase)(title).replace(/[^\w-]+/g, "") || "wp-custom-part";
    const record = {
      title,
      slug: cleanSlug,
      content: (0, import_blocks.serialize)(blocks),
      // `area` is filterable on the server and defaults to `UNCATEGORIZED`
      // if provided value is not allowed.
      area
    };
    const templatePart = await saveEntityRecord(
      "postType",
      "wp_template_part",
      record
    );
    setAttributes({
      slug: templatePart.slug,
      theme: templatePart.theme,
      area: void 0
    });
  };
}
function useTemplatePartArea(area) {
  return (0, import_data.useSelect)(
    (select) => {
      const definedAreas = select(import_core_data.store).getCurrentTheme()?.default_template_part_areas || [];
      const selectedArea = definedAreas.find(
        (definedArea) => definedArea.area === area
      );
      const defaultArea = definedAreas.find(
        (definedArea) => definedArea.area === "uncategorized"
      );
      return {
        icon: selectedArea?.icon || defaultArea?.icon,
        label: selectedArea?.label || (0, import_i18n.__)("Template Part"),
        tagName: selectedArea?.area_tag ?? "div"
      };
    },
    [area]
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useAlternativeBlockPatterns,
  useAlternativeTemplateParts,
  useCreateTemplatePartFromBlocks,
  useTemplatePartArea
});
//# sourceMappingURL=hooks.js.map
