---
import { Font } from 'astro:assets';
import { ClientRouter } from 'astro:transitions';
import { shellRenderBaseLayout, navigationMatchNavPath } from '@publier/native';
import SearchButton from '../components/search-button.astro';
import ThemeToggle from '../components/theme-toggle.astro';
import TopNavMobile from '../components/top-nav-mobile.astro';
import { TopNav } from '../components/index.ts';
import { announcement } from 'virtual:publier-announcement';
import { appearance } from 'virtual:publier-appearance';
import { feeds } from 'virtual:publier-feeds';
import { fonts } from 'virtual:publier-fonts';
import { navConfig, type NavVariant } from 'virtual:publier-nav-config';
import { site } from 'virtual:publier-site';
import { fullPreset, variants } from 'virtual:publier-theme-variants';

interface Props {
	title: string;
	description?: string;
	nav?: string | false;
	navConfig?: NavVariant;
	
	navOverlay?: boolean;
	class?: string;
	lang?: string;
}
const { title, description, nav: navProp, navConfig: resolvedNav, navOverlay = false, class: className, lang = Astro.currentLocale ?? 'en' } = Astro.props;
const currentPath = Astro.url.pathname;
let navVariant: string | false;
if (navProp !== undefined) {
	navVariant = navProp;
} else {
	const matched = navigationMatchNavPath(JSON.stringify(navConfig.pathRules), currentPath);
	navVariant = matched === 'false'
		? false
		: matched === 'null'
			? navConfig.defaultVariant
			: (JSON.parse(matched) as string);
}

const appearanceRules = appearance.pathRules ?? [];
const appearanceMatch = appearanceRules.length
	? navigationMatchNavPath(JSON.stringify(appearanceRules.map((rule, i) => ({ path: rule.path, variant: String(i) }))), currentPath)
	: 'null';
const appearanceRule = appearanceMatch === 'null' || appearanceMatch === 'false' ? undefined : appearanceRules[Number(JSON.parse(appearanceMatch))];
const pageAppearance = {
	default: appearanceRule?.default ?? appearance.default,
	strict: appearanceRule?.strict ?? appearance.strict,
};

const htmlHooks: Record<string, string> = fullPreset
	? Object.fromEntries([['data-full-preset', ''], ...Object.entries(variants).map(([k, v]) => [`data-variant-${k}`, v])])
	: {};
if (navOverlay && navVariant !== false) htmlHooks['data-publier-nav-overlay'] = '';
htmlHooks['data-publier-appearance'] = pageAppearance.default;
htmlHooks['data-publier-appearance-strict'] = String(pageAppearance.strict);

const emailProtectionStart = '<!--email_off-->';
const emailProtectionEnd = '<!--/email_off-->';
const storageKey = announcement?.storageKey ?? 'publier-announcement';

const [head, bodyOpen, bodyClose] = JSON.parse(shellRenderBaseLayout(JSON.stringify({
	title, description, className, defaultScheme: pageAppearance.default, strictScheme: pageAppearance.strict, favicon: site.favicon, footer: site.footer, serviceWorker: !!site.serviceWorker,
	announcement: announcement && { ...announcement, variant: announcement.variant ?? 'info', storageKey, dismissible: true },
}))) as [string, string, string];
---
<!doctype html>
<html lang={lang} {...htmlHooks}>
<head>
<Fragment set:html={head} />
{fonts.faces.map((face) => <Font cssVariable={face} preload />)}
<Font cssVariable="--publier-font-builtin-mono" />
{fonts.slots.includes('heading') && <Font cssVariable="--publier-font-heading" preload />}
{fonts.slots.includes('body') && <Font cssVariable="--publier-font-body" preload />}
{fonts.slots.includes('mono') && <Font cssVariable="--publier-font-mono" />}
{site.url && feeds.map((f) => (
	<link rel="alternate" type="application/rss+xml" title={f.title} href={`${site.url}${f.href}`} />
))}
<ClientRouter />
<slot name="head" />
</head>
<body class={className}>

<Fragment set:html={emailProtectionStart} />

<slot name="skip" />
<Fragment set:html={bodyOpen} />
{navVariant !== false && (
	<TopNav variant={navVariant} variantConfig={resolvedNav} currentPath={currentPath} navVariant={variants.nav}>
		{navConfig.searchEnabled && <SearchButton slot="search" />}
		<ThemeToggle slot="theme" default={pageAppearance.default} strict={pageAppearance.strict} />
		<TopNavMobile slot="mobile" variant={navVariant} variantConfig={resolvedNav} currentPath={currentPath} navVariant={variants.nav} />
	</TopNav>
)}
<slot />
<Fragment set:html={bodyClose} />
<Fragment set:html={emailProtectionEnd} />
</body>
</html>
