import { PageNotFoundError, FragmentNotFoundError, loadRenderInput, routeMetadataForContent, } from '../content/provider.js';
import { normalizeRoute } from '../content/routes.js';
export class FixtureContentProvider {
    siteConfig;
    pages;
    fragments;
    constructor(options = {}) {
        this.siteConfig = cloneFixture(options.siteConfig ?? defaultFixtureSiteConfig());
        this.pages = new Map();
        this.fragments = new Map();
        const inputPages = options.pages ?? { '/': defaultFixturePage() };
        for (const [route, page] of Object.entries(inputPages)) {
            this.pages.set(normalizeRoute(route), cloneFixture(page));
        }
        if (options.fragments) {
            for (const [id, fragment] of Object.entries(options.fragments)) {
                this.fragments.set(id, cloneFixture(fragment));
            }
        }
    }
    async getSiteConfig() {
        return cloneFixture(this.siteConfig);
    }
    async getPage(route) {
        const page = this.pages.get(normalizeRoute(route));
        if (!page) {
            throw new PageNotFoundError(route);
        }
        return cloneFixture(page);
    }
    async getFragment(id) {
        const fragment = this.fragments.get(id);
        if (fragment === undefined) {
            throw new FragmentNotFoundError(id);
        }
        return cloneFixture(fragment);
    }
    async listRoutes() {
        return [...this.pages.entries()]
            .sort(([a], [b]) => a.localeCompare(b))
            .map(([routePath, page]) => ({
            slug: routePath === '/' ? 'home' : routePath.replace(/^\//, ''),
            path: routePath,
            payloadRef: `routes/${routePath === '/' ? 'home' : routePath.replace(/^\//, '')}.json`,
            digest: '',
        }));
    }
    async loadRenderInput(route = '/', options = {}) {
        return loadRenderInput(this, route, options);
    }
    async resolveRouteMetadata(route = '/') {
        const siteConfig = await this.getSiteConfig();
        let page;
        try {
            page = await this.getPage(route);
        }
        catch {
            page = null;
        }
        return routeMetadataForContent(siteConfig, page);
    }
}
export function createFixtureContentProvider(options = {}) {
    return new FixtureContentProvider(options);
}
export function defaultFixtureSiteConfig() {
    return {
        id: 'site_fixture',
        $type: 'site',
        status: 'published',
        title: 'Fixture Site',
        domain: 'www.aci.local',
        defaultLocale: 'en-us',
        seo: {
            title: 'Fixture Site',
            description: 'Fixture content for SDK tests.',
            siteName: 'Fixture Site',
        },
    };
}
export function defaultFixturePage() {
    return {
        id: 'home',
        $type: 'page',
        status: 'published',
        layout: 'marketing',
        renderMode: 'static',
        metadata: {
            title: 'Fixture Home',
            description: 'Fixture home page.',
        },
        regions: {
            main: [],
        },
    };
}
function cloneFixture(value) {
    return JSON.parse(JSON.stringify(value));
}
