"use client";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { useEdenContext } from "./context";
import { sanitizeAbsoluteUrl } from "../utils/security";
function stripHtmlTags(text) {
    return text?.replace(/<[^>]*>?/gm, "") || "";
}
function isPreProdMode(preProd, lang) {
    return Array.isArray(preProd) ? preProd.includes(lang) : preProd;
}
function buildFullTitle(menuPath, subTitle) {
    const bareTitle = stripHtmlTags(menuPath?.title || menuPath?.name);
    return subTitle ? `${bareTitle} ${subTitle}` : bareTitle;
}
function buildPageTitle(menuPath, siteName, seoTitle) {
    const bareSubTitle = stripHtmlTags(menuPath?.subTitle);
    const fullTitle = buildFullTitle(menuPath, bareSubTitle);
    const title = `${fullTitle} - ${siteName}`;
    return seoTitle || title;
}
function buildPath(menuPath) {
    const isIndex = menuPath?.path === "/";
    return isIndex ? menuPath?.path : menuPath?.path?.concat("/");
}
function buildDefaultCanonical(domain, menuPath) {
    const path = buildPath(menuPath);
    if (!domain || !path)
        return null;
    return `https://${domain}${path}`;
}
function buildCanonicalUrl(domain, menuPath, seoCanonical) {
    const defaultCanonical = buildDefaultCanonical(domain, menuPath);
    return sanitizeAbsoluteUrl(seoCanonical || defaultCanonical) || null;
}
function buildAltLangPath(path) {
    const isHome = path === "/";
    return isHome ? path : `${path}/`;
}
function buildAltLangUrl(domain, path) {
    const pathWithEndSlash = buildAltLangPath(path);
    return sanitizeAbsoluteUrl(`https://${domain}${pathWithEndSlash}`) || null;
}
function renderAltLangLink(lang, domain, langSwitch) {
    const { path, disabled } = langSwitch;
    if (!path || disabled)
        return null;
    const url = buildAltLangUrl(domain, path);
    if (!url)
        return null;
    return _jsx("link", { rel: "alternate", hrefLang: lang, href: url }, url);
}
function renderMetaTags(seo, shouldHideFromSearch, edenMode) {
    return (_jsxs(_Fragment, { children: [_jsx("meta", { name: "description", content: seo?.description || "" }), seo?.robots && _jsx("meta", { name: "robots", content: seo.robots }), shouldHideFromSearch && (_jsx("meta", { name: "robots", content: "noindex, nofollow" })), edenMode && _jsx("meta", { name: "google", content: "notranslate" }), _jsx("meta", { name: "format-detection", content: "telephone=no, date=no, email=no, address=no" })] }));
}
function renderAltLangLinks(langs, domains, langSwitches) {
    return langs.map((mappedLang) => {
        const altDomain = domains[mappedLang];
        const langSwitch = langSwitches?.[mappedLang];
        if (!altDomain || !langSwitch)
            return null;
        return renderAltLangLink(mappedLang, altDomain, langSwitch);
    });
}
function buildHeadViewModel({ lang, config, menuPath, seo, domain, }) {
    const { preProd, siteName } = config;
    const inPreProdMode = isPreProdMode(preProd, lang);
    return {
        pageTitle: buildPageTitle(menuPath, siteName, seo?.title),
        canonical: buildCanonicalUrl(domain, menuPath, seo?.canonical),
        shouldHideFromSearch: menuPath?.disabled || inPreProdMode,
    };
}
function buildAltLangLinks({ config, langSwitches, }) {
    const { langs, domains } = config;
    return renderAltLangLinks(langs, domains, langSwitches);
}
function renderCanonicalLink(canonical) {
    if (!canonical)
        return null;
    return _jsx("link", { rel: "canonical", href: canonical });
}
export default function EdenHead() {
    const eden = useEdenContext();
    const { seo, edenMode } = eden;
    const { pageTitle, canonical, shouldHideFromSearch } = buildHeadViewModel(eden);
    const altLangLinks = buildAltLangLinks(eden);
    return (_jsxs("head", { children: [_jsx("title", { children: pageTitle }), renderMetaTags(seo, shouldHideFromSearch, edenMode), renderCanonicalLink(canonical), altLangLinks] }));
}
