import { compareVersion } from "./util/commonFn";

export function judgeEnv() {
    let output = {
        env: '',
        isDowngrade: false
    };
    if (global.document) {
        if (global.__SmallApp_env_run_type) {
            if (global.__SmallApp_env_run_type === 2) {
                output.env = 'Service';
                output.isDowngrade = true;
            } else if (global.__SmallApp_env_run_type === 5) {
                output.env = 'Render';
            } else if (global.__SmallApp_env_run_type === 6) {
                output.env = 'Render';
                output.isDowngrade = true;
            }
        } else {
            output.env = 'Render';
        }
    } else {
        output.env = 'Service';
    }
    return output;
}

export default {
    /**
    * 创建 上下文 CameraContext 对象。
    *
    * @returns {Camera}
    */
    createCameraContext() {
        const _sys = bl.getSystemInfoSync();
        let _isSupport = false;
        if(!judgeEnv().isDowngrade && compareVersion(_sys.SDKVersion, '3.79.0') >= 0 ) {
            _isSupport = true;
        } else {
            if (judgeEnv().isDowngrade) {
                throw new Error('api not supported in webview');
            } else {
                throw new Error(`api not supported in this sdkversion: ${_sys.SDKVersion}`);
            }
        }
        if(_isSupport) {
            const pageId = global.__biliCacheData.getCurrentPageId();
            const biliComponent = global.__biliComponentData;
            if (biliComponent && biliComponent.camera && biliComponent.camera[pageId] && biliComponent.camera[pageId].componentId) {
                if (_sys.platform === 'android') {
                    return global.createCameraContext(pageId, String(biliComponent.camera[pageId].componentId));
                } else {
                    return global.BLGlobal.createCameraContext(pageId, String(biliComponent.camera[pageId].componentId));
                }
            }
        }
    },
};
 