"use strict";

var __decorate = this && this.__decorate || function(decorators, target, key, desc) {
    var d, c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) (d = decorators[i]) && (r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r);
    return c > 3 && r && Object.defineProperty(target, key, r), r;
}, __metadata = this && this.__metadata || function(k, v) {
    if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v);
}, __param = this && this.__param || function(paramIndex, decorator) {
    return function(target, key) {
        decorator(target, key, paramIndex);
    };
}, __awaiter = this && this.__awaiter || function(thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))((function(resolve, reject) {
        function fulfilled(value) {
            try {
                step(generator.next(value));
            } catch (e) {
                reject(e);
            }
        }
        function rejected(value) {
            try {
                step(generator.throw(value));
            } catch (e) {
                reject(e);
            }
        }
        function step(result) {
            var value;
            result.done ? resolve(result.value) : (value = result.value, value instanceof P ? value : new P((function(resolve) {
                resolve(value);
            }))).then(fulfilled, rejected);
        }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    }));
};

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.DefaultGlobal = void 0;

const inversify_lite_1 = require("../common/inversify-lite"), contribution_provider_1 = require("../common/contribution-provider"), tapable_1 = require("../tapable"), constants_1 = require("../constants"), generator_1 = require("../common/generator"), performance_raf_1 = require("../common/performance-raf"), defaultEnv = "browser";

let DefaultGlobal = class {
    get env() {
        return this._env;
    }
    get isImageAnonymous() {
        return this._isImageAnonymous;
    }
    set isImageAnonymous(isImageAnonymous) {
        this._isImageAnonymous = isImageAnonymous;
    }
    get devicePixelRatio() {
        return this._env || this.setEnv("browser"), this.envContribution.getDevicePixelRatio();
    }
    get supportEvent() {
        return this._env || this.setEnv("browser"), this.envContribution.supportEvent;
    }
    set supportEvent(support) {
        this._env || this.setEnv("browser"), this.envContribution.supportEvent = support;
    }
    get supportsTouchEvents() {
        return this._env || this.setEnv("browser"), this.envContribution.supportsTouchEvents;
    }
    set supportsTouchEvents(support) {
        this._env || this.setEnv("browser"), this.envContribution.supportsTouchEvents = support;
    }
    get supportsPointerEvents() {
        return this._env || this.setEnv("browser"), this.envContribution.supportsPointerEvents;
    }
    set supportsPointerEvents(support) {
        this._env || this.setEnv("browser"), this.envContribution.supportsPointerEvents = support;
    }
    get supportsMouseEvents() {
        return this._env || this.setEnv("browser"), this.envContribution.supportsMouseEvents;
    }
    set supportsMouseEvents(support) {
        this._env || this.setEnv("browser"), this.envContribution.supportsMouseEvents = support;
    }
    get applyStyles() {
        return this._env || this.setEnv("browser"), this.envContribution.applyStyles;
    }
    set applyStyles(support) {
        this._env || this.setEnv("browser"), this.envContribution.applyStyles = support;
    }
    constructor(contributions) {
        this.contributions = contributions, this._isImageAnonymous = !0, this._performanceRAFList = [], 
        this.id = generator_1.Generator.GenAutoIncrementId(), this.hooks = {
            onSetEnv: new tapable_1.SyncHook([ "lastEnv", "env", "global" ])
        }, this.measureTextMethod = "native", this.optimizeVisible = !1;
    }
    bindContribution(params) {
        const promiseArr = [];
        if (this.contributions.getContributions().forEach((contribution => {
            const data = contribution.configure(this, params);
            data && data.then && promiseArr.push(data);
        })), promiseArr.length) return Promise.all(promiseArr);
    }
    getDynamicCanvasCount() {
        return this._env || this.setEnv("browser"), this.envContribution.getDynamicCanvasCount();
    }
    getStaticCanvasCount() {
        return this._env || this.setEnv("browser"), this.envContribution.getStaticCanvasCount();
    }
    setEnv(env, params) {
        if (params && !0 === params.force || this._env !== env) return this.deactiveCurrentEnv(), 
        this.activeEnv(env, params);
    }
    deactiveCurrentEnv() {
        this.envContribution && this.envContribution.release();
    }
    activeEnv(env, params) {
        const lastEnv = this._env;
        this._env = env;
        const data = this.bindContribution(params);
        if (data && data.then) return data.then((() => {
            this.envParams = params, this.hooks.onSetEnv.call(lastEnv, env, this);
        }));
        this.envParams = params, this.hooks.onSetEnv.call(lastEnv, env, this);
    }
    setActiveEnvContribution(contribution) {
        this.envContribution = contribution;
    }
    createCanvas(params) {
        return this._env || this.setEnv("browser"), this.envContribution.createCanvas(params);
    }
    createOffscreenCanvas(params) {
        return this._env || this.setEnv("browser"), this.envContribution.createOffscreenCanvas(params);
    }
    releaseCanvas(canvas) {
        return this._env || this.setEnv("browser"), this.envContribution.releaseCanvas(canvas);
    }
    addEventListener(type, listener, options) {
        return this._env || this.setEnv("browser"), this.envContribution.addEventListener(type, listener, options);
    }
    removeEventListener(type, listener, options) {
        return this._env || this.setEnv("browser"), this.envContribution.removeEventListener(type, listener, options);
    }
    dispatchEvent(event) {
        return this._env || this.setEnv("browser"), this.envContribution.dispatchEvent(event);
    }
    getRequestAnimationFrame() {
        return this._env || this.setEnv("browser"), this.envContribution.getRequestAnimationFrame();
    }
    getSpecifiedRequestAnimationFrame(id) {
        this._env || this.setEnv("browser"), this._performanceRAFList[id] || (this._performanceRAFList[id] = new performance_raf_1.PerformanceRAF);
        const performanceRAF = this._performanceRAFList[id];
        return callback => performanceRAF.addAnimationFrameCb(callback);
    }
    getSpecifiedCancelAnimationFrame(id) {
        if (this._env || this.setEnv("browser"), !this._performanceRAFList[id]) return () => !1;
        const performanceRAF = this._performanceRAFList[id];
        return handle => performanceRAF.removeAnimationFrameCb(handle);
    }
    getCancelAnimationFrame() {
        return this._env || this.setEnv("browser"), this.envContribution.getCancelAnimationFrame();
    }
    getElementById(str) {
        return this._env || this.setEnv("browser"), this.envContribution.getElementById ? this.envContribution.getElementById(str) : null;
    }
    getRootElement() {
        return this._env || this.setEnv("browser"), this.envContribution.getRootElement ? this.envContribution.getRootElement() : null;
    }
    getDocument() {
        return this._env || this.setEnv("browser"), this.envContribution.getDocument ? this.envContribution.getDocument() : null;
    }
    mapToCanvasPoint(event, domElement) {
        return this._env || this.setEnv("browser"), this.envContribution.mapToCanvasPoint ? this.envContribution.mapToCanvasPoint(event, domElement) : null;
    }
    loadImage(url) {
        return this._env || this.setEnv("browser"), this.envContribution.loadImage(url);
    }
    loadSvg(str) {
        return this._env || this.setEnv("browser"), this.envContribution.loadSvg(str);
    }
    loadJson(url) {
        return this._env || this.setEnv("browser"), this.envContribution.loadJson(url);
    }
    loadArrayBuffer(url) {
        return this._env || this.setEnv("browser"), this.envContribution.loadArrayBuffer(url);
    }
    loadBlob(url) {
        return this._env || this.setEnv("browser"), this.envContribution.loadBlob(url);
    }
    loadFont(name, source, descriptors) {
        return __awaiter(this, void 0, void 0, (function*() {
            return this._env || this.setEnv("browser"), this.envContribution.loadFont(name, source, descriptors);
        }));
    }
    isChrome() {
        return null != this._isChrome || (this._env || this.setEnv("browser"), this._isChrome = "browser" === this._env && navigator.userAgent.indexOf("Chrome") > -1), 
        this._isChrome;
    }
    isSafari() {
        return null != this._isSafari || (this._env || this.setEnv("browser"), this._isSafari = "browser" === this._env && /Safari/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent)), 
        this._isSafari;
    }
    getNativeAABBBounds(dom) {
        return this._env || this.setEnv("browser"), this.envContribution.getNativeAABBBounds(dom);
    }
    removeDom(dom) {
        return this._env || this.setEnv("browser"), this.envContribution.removeDom(dom);
    }
    createDom(params) {
        return this._env || this.setEnv("browser"), this.envContribution.createDom(params);
    }
    updateDom(dom, params) {
        return this._env || this.setEnv("browser"), this.envContribution.updateDom(dom, params);
    }
    getElementTop(dom, baseWindow = !1) {
        return this._env || this.setEnv("browser"), this.envContribution.getElementTop(dom, baseWindow);
    }
    getElementLeft(dom, baseWindow = !1) {
        return this._env || this.setEnv("browser"), this.envContribution.getElementLeft(dom, baseWindow);
    }
    getElementTopLeft(dom, baseWindow = !1) {
        return this._env || this.setEnv("browser"), this.envContribution.getElementTopLeft(dom, baseWindow);
    }
    isMacOS() {
        return this._env || this.setEnv("browser"), this.envContribution.isMacOS();
    }
    copyToClipBoard(text) {
        return this._env || this.setEnv("browser"), this.envContribution.copyToClipBoard(text);
    }
};

DefaultGlobal = __decorate([ (0, inversify_lite_1.injectable)(), __param(0, (0, 
inversify_lite_1.inject)(contribution_provider_1.ContributionProvider)), __param(0, (0, 
inversify_lite_1.named)(constants_1.EnvContribution)), __metadata("design:paramtypes", [ Object ]) ], DefaultGlobal), 
exports.DefaultGlobal = DefaultGlobal;
//# sourceMappingURL=global.js.map
