import { gradialContentWatchPlugin, gradialDamAssetPlugin, devRefreshPort, devRefreshScriptTag, } from '../dev/index.js';
// ---------------------------------------------------------------------------
// Entries helper — for SvelteKit SSG prerendering
// ---------------------------------------------------------------------------
/**
 * Generates prerender entries from the compiled content manifest for SvelteKit.
 *
 * Usage in svelte.config.js:
 * ```js
 * import { gradialEntries } from '@gradial/aci/sveltekit';
 * export default { kit: { prerender: { entries: await gradialEntries() } } };
 * ```
 *
 * Or in a catch-all `+page.server.ts`:
 * ```ts
 * import { gradialEntries } from '@gradial/aci/sveltekit';
 * export const entries = gradialEntries;
 * ```
 */
export async function gradialEntries() {
    const { FileContentProvider } = await import('../providers/file.js');
    const { normalizeRoute } = await import('../content/routes.js');
    const provider = new FileContentProvider();
    const routes = await provider.listRoutes();
    return routes.map((entry) => {
        const normalized = normalizeRoute(entry.path);
        return normalized === '/' ? '/' : `/${normalized.replace(/^\/|\/$/g, '')}/`;
    });
}
// ---------------------------------------------------------------------------
// SvelteKit Vite plugin
// ---------------------------------------------------------------------------
export function gradialSvelteKit(options = {}) {
    const watchPlugin = gradialContentWatchPlugin(options);
    const damAssetPlugin = gradialDamAssetPlugin(options);
    return {
        ...watchPlugin,
        name: 'gradial-sveltekit',
        configureServer(server) {
            damAssetPlugin.configureServer?.(server);
            watchPlugin.configureServer?.(server);
        },
        transformIndexHtml(html) {
            if (process.env.NODE_ENV === 'production') {
                return html;
            }
            if (devRefreshPort(options) <= 0) {
                return html;
            }
            const script = devRefreshScriptTag(options);
            return html.includes('</body>') ? html.replace('</body>', `${script}</body>`) : `${html}${script}`;
        },
    };
}
