import { getRenderingEngines } from './RenderingEngine/getRenderingEngine';
let csRenderInitialized = false;
import deepMerge from './utilities/deepMerge';
import CentralizedWebWorkerManager from './webWorkerManager/webWorkerManager';
import { getSupportedTextureFormats } from './utilities/textureSupport';
const defaultConfig = {
    gpuTier: { tier: 2 },
    isMobile: false,
    rendering: {
        useCPURendering: false,
        preferSizeOverAccuracy: false,
        strictZSpacingForVolumeViewport: true,
    },
    peerImport: (moduleId) => null,
};
let config = {
    ...defaultConfig,
    rendering: { ...defaultConfig.rendering },
};
let webWorkerManager = null;
let canUseNorm16Texture = false;
function _getGLContext() {
    const canvas = document.createElement('canvas');
    const gl = canvas.getContext('webgl2') ||
        canvas.getContext('webgl') ||
        canvas.getContext('experimental-webgl');
    return gl;
}
function _hasActiveWebGLContext() {
    const gl = _getGLContext();
    return (gl instanceof WebGLRenderingContext || gl instanceof WebGL2RenderingContext);
}
function _hasNorm16TextureSupport() {
    const supportedTextureFormats = getSupportedTextureFormats();
    return supportedTextureFormats.norm16 && supportedTextureFormats.norm16Linear;
}
function isIOS() {
    if (/iPad|iPhone|iPod/.test(navigator.platform)) {
        return true;
    }
    else {
        return (navigator.maxTouchPoints &&
            navigator.maxTouchPoints > 2 &&
            navigator.platform.includes('MacIntel'));
    }
}
function init(configuration = config) {
    if (csRenderInitialized) {
        return csRenderInitialized;
    }
    canUseNorm16Texture = _hasNorm16TextureSupport();
    config = deepMerge(defaultConfig, configuration);
    if (isIOS()) {
        if (configuration.rendering?.preferSizeOverAccuracy) {
            config.rendering.preferSizeOverAccuracy = true;
        }
        else {
            console.log('norm16 texture not supported, you can turn on the preferSizeOverAccuracy flag to use native data type, but be aware of the inaccuracy of the rendering in high bits');
        }
    }
    const hasWebGLContext = _hasActiveWebGLContext();
    if (!hasWebGLContext) {
        console.log('CornerstoneRender: GPU not detected, using CPU rendering');
        config.rendering.useCPURendering = true;
    }
    else {
        console.log('CornerstoneRender: using GPU rendering');
    }
    csRenderInitialized = true;
    if (!webWorkerManager) {
        webWorkerManager = new CentralizedWebWorkerManager();
    }
    return csRenderInitialized;
}
function getCanUseNorm16Texture() {
    return canUseNorm16Texture;
}
function setUseCPURendering(status, updateViewports = true) {
    config.rendering.useCPURendering = status;
    csRenderInitialized = true;
    if (updateViewports) {
        _updateRenderingPipelinesForAllViewports();
    }
}
function setPreferSizeOverAccuracy(status) {
    config.rendering.preferSizeOverAccuracy = status;
    csRenderInitialized = true;
    _updateRenderingPipelinesForAllViewports();
}
function canRenderFloatTextures() {
    if (!isIOS()) {
        return true;
    }
    return false;
}
function resetUseCPURendering() {
    config.rendering.useCPURendering = !_hasActiveWebGLContext();
    _updateRenderingPipelinesForAllViewports();
}
function getShouldUseCPURendering() {
    return config.rendering.useCPURendering;
}
function isCornerstoneInitialized() {
    return csRenderInitialized;
}
function resetInitialization() {
    csRenderInitialized = false;
}
function getConfiguration() {
    return config;
}
function setConfiguration(c) {
    config = c;
    _updateRenderingPipelinesForAllViewports();
}
function _updateRenderingPipelinesForAllViewports() {
    getRenderingEngines().forEach((engine) => {
        engine.getViewports().forEach((viewport) => {
            viewport.updateRenderingPipeline();
        });
    });
}
function getWebWorkerManager() {
    if (!webWorkerManager) {
        webWorkerManager = new CentralizedWebWorkerManager();
    }
    return webWorkerManager;
}
async function peerImport(moduleId) {
    return config.peerImport(moduleId);
}
export { init, getShouldUseCPURendering, isCornerstoneInitialized, setUseCPURendering, setPreferSizeOverAccuracy, resetUseCPURendering, getConfiguration, setConfiguration, getWebWorkerManager, canRenderFloatTextures, peerImport, resetInitialization, getCanUseNorm16Texture, };
