import { ProductionWebGL2Renderer } from './ProductionWebGL2Renderer.js';
import { ProductionWebGPURenderer } from './ProductionWebGPURenderer.js';
export class ProductionRuntimeRenderer {
    renderer;
    backend;
    backendSelection;
    constructor(renderer, backend, backendSelection) {
        this.renderer = renderer;
        this.backend = backend;
        this.backendSelection = backendSelection;
    }
    static async create(options) {
        const selection = resolveProductionRuntimeRendererBackend(options);
        if (selection.selectedBackend === "webgpu") {
            return new ProductionRuntimeRenderer(await ProductionWebGPURenderer.create(options), "webgpu", selection);
        }
        const { backend: _backend, ...webgl2Options } = options;
        return new ProductionRuntimeRenderer(await ProductionWebGL2Renderer.create(webgl2Options), "webgl2", selection);
    }
    renderInteractiveFrame(input) {
        const renderer = this.renderer;
        const result = renderer.renderInteractiveFrame ? renderer.renderInteractiveFrame(input) : renderer.renderFrame(input);
        return withoutReadbackFeatures(result);
    }
    renderFrame(input) {
        return this.renderInteractiveFrame(input);
    }
    resize(width, height) {
        const renderer = this.renderer;
        if (typeof renderer.resize !== "function") {
            throw new Error(`Renderer backend ${this.backend} does not support interactive resizing.`);
        }
        renderer.resize(width, height);
    }
    captureProof(input) {
        const renderer = this.renderer;
        return renderer.captureProof ? renderer.captureProof(input) : renderer.renderImportedAsset(input);
    }
    renderImportedAsset(input) {
        return this.captureProof(input);
    }
    async renderInteractiveFrameAsync(input) {
        const result = this.renderer instanceof ProductionWebGPURenderer
            ? await this.renderer.renderFrameAsync(input)
            : this.renderInteractiveFrame(input);
        return withoutReadbackFeatures(result);
    }
    async renderFrameAsync(input) {
        return this.renderInteractiveFrameAsync(input);
    }
    async captureProofAsync(input) {
        if (this.renderer instanceof ProductionWebGPURenderer) {
            return this.renderer.renderImportedAssetAsync(input);
        }
        return this.captureProof(input);
    }
    async renderImportedAssetAsync(input) {
        return this.captureProofAsync(input);
    }
    getFeatures() {
        return this.renderer.getFeatures();
    }
    getDiagnostics() {
        return this.renderer.getDiagnostics();
    }
    dispose() {
        this.renderer.dispose();
    }
}
export function createProductionRuntimeRenderer(options) {
    return ProductionRuntimeRenderer.create(options);
}
export function resolveProductionRuntimeRendererBackend(options) {
    const browserWebGPU = readBrowserWebGPU();
    const hasWebGPU = Boolean(options.webgpu ?? browserWebGPU);
    const requestedBackend = options.backend ?? (hasWebGPU ? "auto" : "webgl2");
    if (requestedBackend === "webgpu") {
        return {
            requestedBackend,
            selectedBackend: "webgpu",
            asyncRequired: true,
            fallback: false,
            reason: "Explicit backend='webgpu' uses ProductionWebGPURenderer and fails if native WebGPU capabilities are missing."
        };
    }
    if (requestedBackend === "auto") {
        if (hasWebGPU) {
            return {
                requestedBackend,
                selectedBackend: "webgpu",
                asyncRequired: true,
                fallback: false,
                reason: options.webgpu
                    ? "backend='auto' selected WebGPU because a WebGPU runtime object was provided."
                    : "backend='auto' selected WebGPU because navigator.gpu is available in the current browser runtime."
            };
        }
        return {
            requestedBackend,
            selectedBackend: "webgl2",
            asyncRequired: false,
            fallback: true,
            reason: "backend='auto' selected WebGL2 because no WebGPU runtime object was provided to the SDK."
        };
    }
    return {
        requestedBackend,
        selectedBackend: "webgl2",
        asyncRequired: false,
        fallback: false,
        reason: "WebGL2 is selected when no WebGPU runtime is supplied, or when the app explicitly requests backend='webgl2'."
    };
}
function readBrowserWebGPU() {
    const navigatorWithGpu = globalThis.navigator;
    return navigatorWithGpu?.gpu;
}
function withoutReadbackFeatures(result) {
    return {
        ...result,
        features: result.features.filter((feature) => feature.id !== "pixel-readback" && feature.id !== "scene-color-transmission-capture")
    };
}
//# sourceMappingURL=ProductionRuntimeRenderer.js.map