import { getCanonicalPath, getPageDefinition, publishedSitemapRoutes, siteMetadata } from '@/lib/site';
import type { PageFamily, PageRole, PageSeo, ShellTone } from '@/types/site';

export interface ResolvedSeo extends PageSeo {
    canonicalUrl: string;
    family: PageFamily;
    ogDescription: string;
    ogTitle: string;
    pageRole: PageRole;
    robots: string;
    shellTone: ShellTone;
}

export function buildCanonicalUrl(path: string): string {
    return new URL(getCanonicalPath(path), siteMetadata.siteUrl).toString();
}

export function resolveSeo(seo: PageSeo): ResolvedSeo {
    const definition = getPageDefinition(seo.path);

    return {
        ...seo,
        canonicalUrl: buildCanonicalUrl(seo.path),
        family: definition.family,
        ogDescription: seo.ogDescription ?? seo.description,
        ogTitle: seo.ogTitle ?? seo.title,
        pageRole: definition.pageRole,
        robots: 'index,follow',
        shellTone: definition.shellTone,
    };
}

export async function sitemapEntries(): Promise<string[]> {
    return [...publishedSitemapRoutes].map((path) => buildCanonicalUrl(path));
}
