---
import { getEmDashEntry, getSeoMeta, decodeSlug, getSiteSettings } from "emdash";
import { PortableText } from "emdash/ui";
import Base from "../layouts/Base.astro";
import { resolveStarterSiteIdentity } from "../utils/site-identity";

const slug = decodeSlug(Astro.params.slug);

if (!slug) {
	return Astro.redirect("/404");
}

const { entry: page, cacheHint } = await getEmDashEntry("pages", slug);

if (!page) {
	return Astro.redirect("/404");
}

Astro.cache.set(cacheHint);
const { siteTitle } = resolveStarterSiteIdentity(await getSiteSettings());

const seo = getSeoMeta(page, {
	siteTitle,
	siteUrl: Astro.url.origin,
	path: `/${slug}`,
});
---

<Base
	title={seo.title}
	pageTitle={seo.ogTitle}
	description={seo.description}
	canonical={seo.canonical}
	content={{ collection: "pages", id: page.data.id, slug }}
>
	<article>
		<h1 {...page.edit.title}>{page.data.title}</h1>

		<PortableText value={page.data.content} />
	</article>
</Base>
