import { ProductionRuntimeRenderer, resolveProductionRuntimeRendererBackend } from "../production-runtime/ProductionRuntimeRenderer.js";
export class CurrentRoutesInteractiveRenderer {
    renderer;
    canvas;
    backend;
    backendSelection;
    metrics = createCurrentRoutesRuntimeMetrics();
    constructor(renderer, canvas) {
        this.renderer = renderer;
        this.canvas = canvas;
        this.backend = renderer.backend;
        this.backendSelection = renderer.backendSelection;
    }
    static async create(options) {
        const selection = resolveProductionRuntimeRendererBackend(options);
        const renderer = await ProductionRuntimeRenderer.create({
            ...options,
            backend: selection.requestedBackend
        });
        return new CurrentRoutesInteractiveRenderer(renderer, options.canvas);
    }
    async renderFrame(input) {
        const started = now();
        const result = await this.renderer.renderInteractiveFrameAsync(input);
        this.metrics.record({
            frameMs: now() - started,
            renderMs: result.timing?.renderMs ?? 0,
            diagnostics: result.diagnostics,
            backend: result.backend
        });
        return result;
    }
    resize(width, height) {
        this.renderer.resize(width, height);
    }
    getDiagnostics() {
        return this.renderer.getDiagnostics();
    }
    getMetrics() {
        return this.metrics.snapshot(this.backend);
    }
    screenshot() {
        return captureCurrentRoutesCanvasScreenshot(this.canvas);
    }
    dispose() {
        this.renderer.dispose();
    }
}
export function createCurrentRoutesInteractiveRenderer(options) {
    return CurrentRoutesInteractiveRenderer.create(options);
}
export function createCurrentRoutesPostprocess(exposure) {
    return {
        targetFormat: "rgba16f",
        toneMapping: {
            operator: "filmic",
            exposure: clamp(exposure, 0.1, 4),
            whitePoint: 1.25,
            inputColorSpace: "linear",
            outputColorSpace: "srgb"
        },
        colorGrade: {
            contrast: 1.08,
            saturation: 1.05,
            vibrance: 0.1,
            vignette: 0.12,
            sharpening: 0.22
        },
        bloom: {
            threshold: 0.92,
            intensity: 0.08,
            radius: 1
        },
        fxaa: {
            edgeThreshold: 0.08,
            subpixelBlend: 0.55
        }
    };
}
export function captureCurrentRoutesCanvasScreenshot(canvas) {
    if (!("toDataURL" in canvas) || typeof canvas.toDataURL !== "function") {
        throw new Error("CurrentRoutes screenshot capture requires an HTMLCanvasElement with preserveDrawingBuffer enabled.");
    }
    return {
        mimeType: "image/png",
        dataUrl: canvas.toDataURL("image/png"),
        width: canvas.width,
        height: canvas.height
    };
}
function createCurrentRoutesRuntimeMetrics() {
    let frameCount = 0;
    let lastFrameMs = 0;
    let averageFrameMs = 0;
    let lastRenderMs = 0;
    let averageRenderMs = 0;
    let diagnostics = { drawCalls: 0, buffers: 0, shaders: 0, lastError: null, contextLost: false };
    return {
        record(sample) {
            frameCount += 1;
            lastFrameMs = sample.frameMs;
            lastRenderMs = sample.renderMs;
            averageFrameMs += (sample.frameMs - averageFrameMs) / frameCount;
            averageRenderMs += (sample.renderMs - averageRenderMs) / frameCount;
            diagnostics = sample.diagnostics;
        },
        snapshot(backend) {
            return {
                frameCount,
                lastFrameMs: round(lastFrameMs),
                averageFrameMs: round(averageFrameMs),
                lastRenderMs: round(lastRenderMs),
                averageRenderMs: round(averageRenderMs),
                drawCalls: diagnostics.drawCalls,
                textures: diagnostics.textures ?? 0,
                buffers: diagnostics.buffers,
                shaders: diagnostics.shaders,
                backend,
                ...(diagnostics.stateCacheIssued !== undefined ? { stateCacheIssued: diagnostics.stateCacheIssued } : {}),
                ...(diagnostics.stateCacheSkipped !== undefined ? { stateCacheSkipped: diagnostics.stateCacheSkipped } : {}),
                ...(diagnostics.stateCacheProgramSwitches !== undefined ? { stateCacheProgramSwitches: diagnostics.stateCacheProgramSwitches } : {}),
                ...(diagnostics.stateCacheTextureBinds !== undefined ? { stateCacheTextureBinds: diagnostics.stateCacheTextureBinds } : {}),
                ...(diagnostics.stateCacheBufferBinds !== undefined ? { stateCacheBufferBinds: diagnostics.stateCacheBufferBinds } : {}),
                ...(diagnostics.stateCacheVertexArrayBinds !== undefined ? { stateCacheVertexArrayBinds: diagnostics.stateCacheVertexArrayBinds } : {}),
                ...(diagnostics.stateCacheSamplerBinds !== undefined ? { stateCacheSamplerBinds: diagnostics.stateCacheSamplerBinds } : {})
            };
        }
    };
}
function now() {
    return typeof performance !== "undefined" ? performance.now() : Date.now();
}
function round(value) {
    return Math.round(value * 100) / 100;
}
function clamp(value, min, max) {
    return Math.min(max, Math.max(min, value));
}
//# sourceMappingURL=index.js.map