import { normalizeRoute } from './routes.js';
// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------
export class PageNotFoundError extends Error {
    constructor(route, cause) {
        super(`Page not found for route ${route}`);
        this.name = 'PageNotFoundError';
        this.cause = cause;
    }
}
export class FragmentNotFoundError extends Error {
    constructor(id, cause) {
        super(`Fragment not found: ${id}`);
        this.name = 'FragmentNotFoundError';
        this.cause = cause;
    }
}
export async function loadRenderInput(provider, route = '/', options = {}) {
    const normalized = normalizeRoute(route);
    const [siteConfig, page] = await Promise.all([
        provider.getSiteConfig(),
        provider.getPage(normalized).catch((error) => {
            if (error instanceof PageNotFoundError)
                return null;
            throw error;
        })
    ]);
    return {
        route: normalized,
        domain: options.domain || siteConfig.domain,
        locale: (options.locale || siteConfig.defaultLocale || 'en-us').toLowerCase(),
        siteConfig,
        page
    };
}
export function routeMetadataForContent(siteConfig, page) {
    const title = page?.metadata?.title || siteConfig.seo?.title || siteConfig.title;
    const description = page?.metadata?.description || siteConfig.seo?.description || '';
    return {
        title,
        description,
        canonical: page?.metadata?.canonical,
        siteName: siteConfig.seo?.siteName || siteConfig.title
    };
}
