import { __awaiter, __extends, __generator } from "tslib";
self['CESIUM_BASE_URL'] =
    self['CESIUM_BASE_URL'] ||
        '//static.gis-cdn.msign.net/assets/angularis_js_api/1.71-0.2/';
import { Viewer, SkyBox, Color, RequestScheduler, UrlTemplateImageryProvider, WebMercatorTilingScheme, CesiumTerrainProvider, FeatureDetection } from '@taotieh/cesium_extend';
import { tiles } from './Assets';
import Control from './_Control';
import '../core/CesiumFix';
// require('cesium/style.css');
var img_sat_google_wgs84 = tiles.img_sat_google_wgs84, trr_world = tiles.trr_world;
var CesiumControl = /** @class */ (function (_super) {
    __extends(CesiumControl, _super);
    function CesiumControl(inst, container, option) {
        if (option === void 0) { option = {}; }
        var _this = _super.call(this, inst) || this;
        // console.log(Ion, 'Ion')
        //   Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJkODhiMzU5YS0wYzI3LTRlNDItOTlkMC1jZmM1NGMyOThiZjkiLCJpZCI6MjU5LCJzY29wZXMiOlsiYXNyIiwiZ2MiXSwiaWF0IjoxNTkxMDQ3MTIzfQ.tdCE-sqNw6_6LY-j2jk035vpzEmVuAY3ajtBBpLDxuM';
        var noSky = new SkyBox({});
        var viewer = (_this._viewer = new Viewer(container, {
            animation: false,
            baseLayerPicker: false,
            creditContainer: document.createElement('div'),
            contextOptions: {
                webgl: {
                    alpha: true,
                    preserveDrawingBuffer: true,
                },
            },
            geocoder: false,
            homeButton: false,
            maximumRenderTimeChange: 4,
            navigationHelpButton: false,
            sceneModePicker: false,
            selectionIndicator: false,
            skyBox: noSky,
            timeline: false,
            skyAtmosphere: false,
            imageryProvider: new UrlTemplateImageryProvider({
                url: img_sat_google_wgs84,
                credit: '',
                tilingScheme: new WebMercatorTilingScheme(),
                maximumLevel: 21,
            }),
            terrainProvider: new CesiumTerrainProvider({
                url: trr_world,
                requestWaterMask: true,
                requestVertexNormals: true,
            }),
        }));
        viewer.resolutionScale = 1;
        viewer.scene.globe.baseColor = new Color(0, 0, 0, 0);
        viewer.scene.backgroundColor = new Color(0, 0, 0, 0);
        viewer.scene.globe.imageryLayers.get(0).alpha = 0.5;
        viewer.scene.highDynamicRange = false;
        // viewer.scene.globe && (viewer.scene.globe.show = false);
        viewer.scene.sun && (viewer.scene.sun.show = false);
        viewer.scene.moon && (viewer.scene.moon.show = false);
        viewer._cesiumWidget._supportsImageRenderingPixelated = FeatureDetection.supportsImageRenderingPixelated();
        viewer._cesiumWidget._forceResize = true;
        if (FeatureDetection.supportsImageRenderingPixelated()) {
            var vtxf_dpr = window.devicePixelRatio;
            // 适度降低分辨率
            while (vtxf_dpr >= 2.0) {
                vtxf_dpr /= 2.0;
            }
            //alert(dpr);
            viewer.resolutionScale = vtxf_dpr;
        }
        return _this;
    }
    /** *******************************************************************************
     *  Cesium 库必须异步引入, 这并不是单纯为了切分代码.
     *  因为 Cesium 在导入时会自动执行, 并读取 window.requestAnimationFrame 函数
     *
     *  检视 ./Loop.ts 里的代码可明白
     *  应用层在初始化的时候会尝试替换默认的 requestAnimationFrame 函数, 以实现底层控制.
     *
     *  需要等应用层初始化好(即不可以同步方式导入 Cesium 依赖). 才能导入 Cesium 库.
     **********************************************************************************/
    CesiumControl.prototype._fetchCesium = function () {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, import('../core/CesiumFix')];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    CesiumControl.constructorAsync = function (inst, container, option) {
        if (option === void 0) { option = {}; }
        return __awaiter(this, void 0, void 0, function () {
            var that;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (container) {
                            inst.resetInstance();
                        }
                        that = new CesiumControl(inst, container, option);
                        return [4 /*yield*/, that._fetchCesium()];
                    case 1:
                        _a.sent();
                        return [2 /*return*/, that];
                }
            });
        });
    };
    Object.defineProperty(CesiumControl.prototype, "viewer", {
        get: function () {
            return this._viewer;
        },
        enumerable: false,
        configurable: true
    });
    CesiumControl.prototype.bustMode = function (bust) {
        if (bust === void 0) { bust = true; }
        if (bust) {
            RequestScheduler.maximumRequestsPerServer = 256;
            RequestScheduler.maximumRequests = 512;
        }
        else {
            RequestScheduler.maximumRequestsPerServer = 6;
            RequestScheduler.maximumRequests = 50;
        }
    };
    return CesiumControl;
}(Control));
export default CesiumControl;
