import { useSelect } from "@wordpress/data";
import { useMemo } from "@wordpress/element";
import { store as coreStore } from "@wordpress/core-data";
import { store as editorStore } from "../../store";
function useEditedPostContext() {
  return useSelect((select) => {
    const { getCurrentPostId, getCurrentPostType } = select(editorStore);
    return {
      postId: getCurrentPostId(),
      postType: getCurrentPostType()
    };
  }, []);
}
function useAllowSwitchingTemplates() {
  const { postType, postId } = useEditedPostContext();
  return useSelect(
    (select) => {
      const { canUser, getEntityRecord, getEntityRecords } = select(coreStore);
      const siteSettings = canUser("read", {
        kind: "root",
        name: "site"
      }) ? getEntityRecord("root", "site") : void 0;
      const isPostsPage = +postId === siteSettings?.page_for_posts;
      const isFrontPage = postType === "page" && +postId === siteSettings?.page_on_front;
      const templates = isFrontPage ? getEntityRecords("postType", "wp_template", {
        per_page: -1
      }) : [];
      const hasFrontPage = isFrontPage && !!templates?.some(({ slug }) => slug === "front-page");
      return !isPostsPage && !hasFrontPage;
    },
    [postId, postType]
  );
}
function useTemplates(postType) {
  const { defaultTemplateTypes, registeredTemplates, userTemplates } = useSelect(
    (select) => {
      return {
        defaultTemplateTypes: select(coreStore).getCurrentTheme()?.default_template_types,
        registeredTemplates: select(coreStore).getEntityRecords(
          "postType",
          "wp_registered_template",
          {
            per_page: -1,
            post_type: postType
          }
        ),
        userTemplates: select(coreStore).getEntityRecords(
          "postType",
          "wp_template",
          { per_page: -1, combinedTemplates: false }
        )
      };
    },
    [postType]
  );
  return useMemo(() => {
    if (!defaultTemplateTypes || !registeredTemplates || !userTemplates) {
      return [];
    }
    return [
      ...registeredTemplates,
      ...userTemplates.filter(
        (template) => (
          // Only give "custom" templates as an option, which
          // means the is_wp_suggestion meta field is not set and
          // the slug is not found in the default template types.
          // https://github.com/WordPress/wordpress-develop/blob/97382397b2bd7c85aef6d4cd1c10bafd397957fc/src/wp-includes/block-template-utils.php#L858-L867
          !template.meta.is_wp_suggestion && !defaultTemplateTypes.find(
            (type) => type.slug === template.slug
          )
        )
      )
    ];
  }, [registeredTemplates, userTemplates, defaultTemplateTypes]);
}
function useAvailableTemplates(postType) {
  const currentTemplateSlug = useCurrentTemplateSlug();
  const allowSwitchingTemplate = useAllowSwitchingTemplates();
  const templates = useTemplates(postType);
  return useMemo(
    () => allowSwitchingTemplate && templates?.filter(
      (template) => (template.is_custom || template.type === "wp_template") && template.slug !== currentTemplateSlug && !!template.content.raw
      // Skip empty templates.
    ),
    [templates, currentTemplateSlug, allowSwitchingTemplate]
  );
}
function useCurrentTemplateSlug() {
  const { postType, postId } = useEditedPostContext();
  const templates = useTemplates(postType);
  const entityTemplate = useSelect(
    (select) => {
      const post = select(coreStore).getEditedEntityRecord(
        "postType",
        postType,
        postId
      );
      return post?.template;
    },
    [postType, postId]
  );
  if (!entityTemplate) {
    return;
  }
  return templates?.find((template) => template.slug === entityTemplate)?.slug;
}
export {
  useAllowSwitchingTemplates,
  useAvailableTemplates,
  useCurrentTemplateSlug,
  useEditedPostContext
};
//# sourceMappingURL=hooks.js.map
