// packages/block-library/src/template-part/edit/utils/hooks.js
import { paramCase as kebabCase } from "change-case";
import { useDispatch, useSelect } from "@wordpress/data";
import { store as coreStore } from "@wordpress/core-data";
import { store as blockEditorStore } from "@wordpress/block-editor";
import { useMemo } from "@wordpress/element";
import { serialize } from "@wordpress/blocks";
import { __ } from "@wordpress/i18n";
import { createTemplatePartId } from "./create-template-part-id";
function useAlternativeTemplateParts(area, excludedId) {
  const { templateParts, isResolving } = useSelect((select) => {
    const { getEntityRecords, isResolving: _isResolving } = select(coreStore);
    const query = { per_page: -1 };
    return {
      templateParts: getEntityRecords(
        "postType",
        "wp_template_part",
        query
      ),
      isResolving: _isResolving("getEntityRecords", [
        "postType",
        "wp_template_part",
        query
      ])
    };
  }, []);
  const filteredTemplateParts = useMemo(() => {
    if (!templateParts) {
      return [];
    }
    return templateParts.filter(
      (templatePart) => createTemplatePartId(
        templatePart.theme,
        templatePart.slug
      ) !== excludedId && (!area || "uncategorized" === area || templatePart.area === area)
    ) || [];
  }, [templateParts, area, excludedId]);
  return {
    templateParts: filteredTemplateParts,
    isResolving
  };
}
function useAlternativeBlockPatterns(area, clientId) {
  return useSelect(
    (select) => {
      const blockNameWithArea = area ? `core/template-part/${area}` : "core/template-part";
      const { getBlockRootClientId, getPatternsByBlockTypes } = select(blockEditorStore);
      const rootClientId = getBlockRootClientId(clientId);
      return getPatternsByBlockTypes(blockNameWithArea, rootClientId);
    },
    [area, clientId]
  );
}
function useCreateTemplatePartFromBlocks(area, setAttributes) {
  const { saveEntityRecord } = useDispatch(coreStore);
  return async (blocks = [], title = __("Untitled Template Part")) => {
    const cleanSlug = kebabCase(title).replace(/[^\w-]+/g, "") || "wp-custom-part";
    const record = {
      title,
      slug: cleanSlug,
      content: 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 useSelect(
    (select) => {
      const definedAreas = select(coreStore).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 || __("Template Part"),
        tagName: selectedArea?.area_tag ?? "div"
      };
    },
    [area]
  );
}
export {
  useAlternativeBlockPatterns,
  useAlternativeTemplateParts,
  useCreateTemplatePartFromBlocks,
  useTemplatePartArea
};
//# sourceMappingURL=hooks.js.map
