import { resolve } from 'node:path';
import { existsSync, mkdirSync, readFileSync } from 'node:fs';
import { createHash } from 'node:crypto';
/**
 * Build the preview runtime — a small Svelte app that includes the user's
 * theme (layouts, components, CSS) — using Vite's programmatic API.
 *
 * The built output is cached by theme path hash so repeat sessions are instant.
 * If Vite or @sveltejs/vite-plugin-svelte aren't available (e.g. non-SvelteKit
 * project), returns { success: false } and the editor falls back to HTML preview.
 */
export async function buildPreviewRuntime(themeSveltePath) {
    const cacheDir = resolve(import.meta.dirname, '..', '.preview-cache');
    const runtimeDir = resolve(import.meta.dirname, '..', 'preview-runtime');
    // Cache key includes theme path + runtime source content so any source change busts the cache
    const hasher = createHash('md5').update(themeSveltePath);
    for (const file of ['App.svelte', 'index.html']) {
        try {
            hasher.update(readFileSync(resolve(runtimeDir, file)));
        }
        catch { /**/ }
    }
    const hash = hasher.digest('hex').slice(0, 8);
    const outputDir = resolve(cacheDir, hash);
    // Skip rebuild if cache exists
    if (existsSync(resolve(outputDir, 'index.html'))) {
        return { outputDir, success: true };
    }
    try {
        // Dynamic imports — these may not be installed at runtime
        const { build } = await import('vite');
        const { svelte } = await import('@sveltejs/vite-plugin-svelte');
        if (!existsSync(resolve(runtimeDir, 'index.html'))) {
            console.warn('Preview runtime source files not found');
            return { outputDir: '', success: false };
        }
        mkdirSync(outputDir, { recursive: true });
        await build({
            root: runtimeDir,
            base: '/preview/',
            plugins: [
                svelte({ compilerOptions: { css: 'injected' } }),
                refraktThemePlugin(themeSveltePath),
            ],
            build: {
                outDir: outputDir,
                emptyOutDir: true,
                rollupOptions: {
                    input: resolve(runtimeDir, 'index.html'),
                },
            },
            resolve: {
                dedupe: ['svelte', '@refrakt-md/svelte', '@refrakt-md/behaviors'],
            },
            logLevel: 'warn',
        });
        return { outputDir, success: true };
    }
    catch (err) {
        console.warn('Preview runtime build failed (falling back to HTML preview):', err.message);
        return { outputDir: '', success: false };
    }
}
/**
 * Vite plugin that provides the user's theme via `virtual:refrakt-theme`.
 * The preview runtime App.svelte imports from this virtual module.
 */
function refraktThemePlugin(themeSveltePath) {
    const virtualId = 'virtual:refrakt-theme';
    const resolvedVirtualId = '\0' + virtualId;
    return {
        name: 'refrakt-preview-theme',
        resolveId(id) {
            if (id === virtualId)
                return resolvedVirtualId;
        },
        load(id) {
            if (id === resolvedVirtualId) {
                // Re-export the theme from the user's theme package
                return `export { theme } from '${themeSveltePath}';`;
            }
        },
    };
}
//# sourceMappingURL=preview-builder.js.map