import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { useMemo, useSyncExternalStore } from "react";
import { useMakeswiftHostApiClient } from "../../host-api-client";
import { useFrameworkContext } from "../hooks/use-framework-context";
import { useIsInBuilder } from "../../hooks/use-is-in-builder";
import { usePageSnippets } from "../hooks/use-page-snippets";
import { PageTitle, PageMeta, PageLink, PageStyle } from "./head-tags";
const defaultFavicon = {
  id: "default-favicon",
  mimetype: "image/png",
  publicUrl: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACIAAAAiCAYAAAA6RwvCAAAACXBIWXMAABcRAAAXEQHKJvM/AAABjElEQVRYhc2XzU3EMBCFB8TddAAXn6EE6GCpgNABZ1/IXnymBOgAOmA7YM8+ABVsXEHQQFaKQryeN3Yk3ilKJtEnv/nLUd/3pFG0riGi88yrnQn+UfJ5FUi0riWiB2H4nQn+KRd0DFP8agXEfkqCYJBoHdtxIQxfm+DfFgEhoith3NYE30o/qgGR2BJB+xY7kdYEL8oNFUi0jiFMJuxVWrJqEMFxsyUNCsE6AeNztvBp7aJ143vXksoRnwhYtmNdSoIQa6RlO9YXEWW7KgoCleOgxgTf1QZBT+RZ2lXFING6UxCCq+ceeUE8fYdknY599v9sJvzGBP+yCEgC7GPmETc0OJ+0awAlkhe2pAbIXAeFZ8xe2g2Nk3c3ub0xwWt6zY9qbmiqGVMbZK21ZC/YmhlbeBMTzZNDQqcvDb1kM1x32iqZSt1HaqukfKvq34BAOTLsrH+ETNmUkKHHA+428RgeclPVWozeSyAI2EdWB34jtqXNTAySOY3i/KgFIlqOa4GkFmBegorzg4joG07he/M7zl6jAAAAAElFTkSuQmCC"
};
function PageHead({ document: page, metadata = {} }) {
  const {
    title: useTitle = false,
    favicon: useFavicon = false,
    canonicalUrl: useCanonicalUrl = false,
    indexingBlocked: useIndexingBlocked = false,
    description: useDescription = false,
    keywords: useKeywords = false,
    socialImage: useSocialImage = false
  } = metadata;
  const { headSnippets } = usePageSnippets({ page });
  const { HeadSnippet } = useFrameworkContext();
  const isInBuilder = useIsInBuilder();
  const site = useCachedSite(isInBuilder ? page.site.id : null);
  const baseLocalizedPage = page.localizedPages.find(({ parentId }) => parentId == null);
  const favicon = page.meta.favicon ?? defaultFavicon;
  const title = baseLocalizedPage?.meta.title ?? page.meta.title;
  const description = baseLocalizedPage?.meta.description ?? page.meta.description;
  const keywords = baseLocalizedPage?.meta.keywords ?? page.meta.keywords;
  const socialImage = baseLocalizedPage?.meta.socialImage ?? page.meta.socialImage;
  const canonicalUrl = baseLocalizedPage?.seo.canonicalUrl ?? page.seo.canonicalUrl;
  const isIndexingBlocked = baseLocalizedPage?.seo.isIndexingBlocked ?? page.seo.isIndexingBlocked;
  const fontFamilyParamValue = useMemo(() => {
    if (site == null) {
      return page.fonts.map(({ family, variants }) => {
        return `${family.replace(/ /g, "+")}:${variants.join()}`;
      }).join("|");
    }
    return site.googleFonts.edges.filter((edge) => edge != null).map(({ activeVariants, node: { family, variants } }) => {
      const activeVariantSpecifiers = variants.filter(
        (variant) => activeVariants.some((activeVariant) => activeVariant.specifier === variant.specifier)
      ).map((variant) => variant.specifier).join();
      return `${family.replace(/ /g, "+")}:${activeVariantSpecifiers}`;
    }).join("|");
  }, [site, page]);
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(PageStyle, { precedence: "high", href: "makeswift-base-styles", children: `
        html {
            font-family: sans-serif;
        }
        div#__next {
            overflow: hidden;
        }
        ` }),
    useTitle && title && /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(PageTitle, { children: title }),
      /* @__PURE__ */ jsx(PageMeta, { property: "og:title", content: title }),
      /* @__PURE__ */ jsx(PageMeta, { name: "twitter:title", content: title })
    ] }),
    useFavicon && favicon && /* @__PURE__ */ jsx(PageLink, { rel: "icon", type: favicon.mimetype, href: favicon.publicUrl }),
    useCanonicalUrl && canonicalUrl && /* @__PURE__ */ jsx(PageLink, { rel: "canonical", href: canonicalUrl }),
    useIndexingBlocked && isIndexingBlocked && /* @__PURE__ */ jsx(PageMeta, { name: "robots", content: "noindex" }),
    useDescription && description && /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(PageMeta, { name: "description", content: description }),
      /* @__PURE__ */ jsx(PageMeta, { property: "og:description", content: description }),
      /* @__PURE__ */ jsx(PageMeta, { name: "twitter:description", content: description })
    ] }),
    useKeywords && keywords && /* @__PURE__ */ jsx(PageMeta, { name: "keywords", content: keywords }),
    useSocialImage && socialImage && /* @__PURE__ */ jsxs(Fragment, { children: [
      /* @__PURE__ */ jsx(PageMeta, { property: "og:image", content: socialImage.publicUrl }),
      /* @__PURE__ */ jsx(PageMeta, { property: "og:image:type", content: socialImage.mimetype }),
      /* @__PURE__ */ jsx(PageMeta, { name: "twitter:image", content: socialImage.publicUrl }),
      /* @__PURE__ */ jsx(PageMeta, { name: "twitter:card", content: "summary_large_image" })
    ] }),
    fontFamilyParamValue !== "" && /* @__PURE__ */ jsx(
      PageLink,
      {
        precedence: "medium",
        rel: "stylesheet",
        href: `https://fonts.googleapis.com/css?family=${fontFamilyParamValue}&display=swap`
      }
    ),
    headSnippets.map((snippet) => /* @__PURE__ */ jsx(HeadSnippet, { snippet }, snippet.id))
  ] });
}
function useCachedSite(siteId) {
  const client = useMakeswiftHostApiClient();
  const getSnapshot = () => siteId == null ? null : client.readSite(siteId);
  const site = useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
  return site;
}
export {
  PageHead
};
//# sourceMappingURL=PageHead.js.map