"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);
var PageHead_exports = {};
__export(PageHead_exports, {
  PageHead: () => PageHead
});
module.exports = __toCommonJS(PageHead_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_host_api_client = require("../../host-api-client");
var import_use_framework_context = require("../hooks/use-framework-context");
var import_use_is_in_builder = require("../../hooks/use-is-in-builder");
var import_use_page_snippets = require("../hooks/use-page-snippets");
var import_head_tags = require("./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 } = (0, import_use_page_snippets.usePageSnippets)({ page });
  const { HeadSnippet } = (0, import_use_framework_context.useFrameworkContext)();
  const isInBuilder = (0, import_use_is_in_builder.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 = (0, import_react.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__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageStyle, { precedence: "high", href: "makeswift-base-styles", children: `
        html {
            font-family: sans-serif;
        }
        div#__next {
            overflow: hidden;
        }
        ` }),
    useTitle && title && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageTitle, { children: title }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageMeta, { property: "og:title", content: title }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageMeta, { name: "twitter:title", content: title })
    ] }),
    useFavicon && favicon && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageLink, { rel: "icon", type: favicon.mimetype, href: favicon.publicUrl }),
    useCanonicalUrl && canonicalUrl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageLink, { rel: "canonical", href: canonicalUrl }),
    useIndexingBlocked && isIndexingBlocked && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageMeta, { name: "robots", content: "noindex" }),
    useDescription && description && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageMeta, { name: "description", content: description }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageMeta, { property: "og:description", content: description }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageMeta, { name: "twitter:description", content: description })
    ] }),
    useKeywords && keywords && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageMeta, { name: "keywords", content: keywords }),
    useSocialImage && socialImage && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageMeta, { property: "og:image", content: socialImage.publicUrl }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageMeta, { property: "og:image:type", content: socialImage.mimetype }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageMeta, { name: "twitter:image", content: socialImage.publicUrl }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_head_tags.PageMeta, { name: "twitter:card", content: "summary_large_image" })
    ] }),
    fontFamilyParamValue !== "" && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_head_tags.PageLink,
      {
        precedence: "medium",
        rel: "stylesheet",
        href: `https://fonts.googleapis.com/css?family=${fontFamilyParamValue}&display=swap`
      }
    ),
    headSnippets.map((snippet) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(HeadSnippet, { snippet }, snippet.id))
  ] });
}
function useCachedSite(siteId) {
  const client = (0, import_host_api_client.useMakeswiftHostApiClient)();
  const getSnapshot = () => siteId == null ? null : client.readSite(siteId);
  const site = (0, import_react.useSyncExternalStore)(client.subscribe, getSnapshot, getSnapshot);
  return site;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  PageHead
});
//# sourceMappingURL=PageHead.js.map