var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "../App", "../core/AtomOnce", "../core/AtomUri", "../di/ServiceCollection", "../services/BusyIndicatorService", "../services/NavigationService", "./core/AtomUI", "./services/WebBusyIndicatorService", "./services/WindowService", "./styles/AtomStyleSheet", "./styles/AtomTheme"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var App_1 = require("../App");
    var AtomOnce_1 = require("../core/AtomOnce");
    var AtomUri_1 = require("../core/AtomUri");
    var ServiceCollection_1 = require("../di/ServiceCollection");
    var BusyIndicatorService_1 = require("../services/BusyIndicatorService");
    var NavigationService_1 = require("../services/NavigationService");
    var AtomUI_1 = require("./core/AtomUI");
    var WebBusyIndicatorService_1 = require("./services/WebBusyIndicatorService");
    var WindowService_1 = require("./services/WindowService");
    var AtomStyleSheet_1 = require("./styles/AtomStyleSheet");
    var AtomTheme_1 = require("./styles/AtomTheme");
    var WebApp = /** @class */ (function (_super) {
        __extends(WebApp, _super);
        function WebApp() {
            var _this = _super.call(this) || this;
            _this.mContextId = 1;
            _this.hashUpdater = new AtomOnce_1.AtomOnce();
            _this.url = new AtomUri_1.AtomUri(location.href);
            _this.put(NavigationService_1.NavigationService, _this.resolve(WindowService_1.WindowService));
            _this.put(WebApp, _this);
            _this.put(BusyIndicatorService_1.BusyIndicatorService, _this.resolve(WebBusyIndicatorService_1.WebBusyIndicatorService));
            ServiceCollection_1.ServiceCollection.instance.registerSingleton(AtomStyleSheet_1.AtomStyleSheet, function (sp) { return sp.resolve(AtomTheme_1.AtomTheme); });
            // let us set contextId
            _this.mContextId = parseInt((_this.url.hash.contextId || "0").toString(), 10);
            if (!_this.mContextId) {
                //  create new context Id in session...
                for (var index = 0; index < 100; index++) {
                    var cid = "contextId" + index;
                    var cidData = sessionStorage.getItem("contextId" + index);
                    if (!cidData) {
                        _this.mContextId = index;
                        sessionStorage.setItem(cid, cid);
                        _this.url.hash.contextId = index;
                        _this.syncUrl();
                        break;
                    }
                }
            }
            window.addEventListener("hashchange", function () {
                _this.hashUpdater.run(function () {
                    _this.url = new AtomUri_1.AtomUri(location.href);
                });
            });
            // registering font awesome
            _this.installStyleSheet("https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.9.0/css/all.css");
            _this.installStyleSheet({
                href: "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css",
                integrity: "sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T",
                crossOrigin: "anonymous"
            });
            return _this;
        }
        Object.defineProperty(WebApp.prototype, "parentElement", {
            get: function () {
                return document.body;
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(WebApp.prototype, "root", {
            get: function () {
                return this.mRoot;
            },
            set: function (v) {
                var old = this.mRoot;
                if (old) {
                    old.dispose();
                }
                this.mRoot = v;
                if (!v) {
                    return;
                }
                var pe = this.parentElement;
                var ce = new AtomUI_1.ChildEnumerator(pe);
                var de = [];
                while (ce.next()) {
                    de.push(ce.current);
                }
                for (var _i = 0, de_1 = de; _i < de_1.length; _i++) {
                    var iterator = de_1[_i];
                    iterator.remove();
                }
                pe.appendChild(v.element);
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(WebApp.prototype, "theme", {
            get: function () {
                return this.get(AtomStyleSheet_1.AtomStyleSheet);
            },
            set: function (v) {
                this.put(AtomTheme_1.AtomTheme, v);
                this.put(AtomStyleSheet_1.AtomStyleSheet, v);
            },
            enumerable: false,
            configurable: true
        });
        Object.defineProperty(WebApp.prototype, "contextId", {
            get: function () {
                return "contextId_" + this.mContextId;
            },
            enumerable: false,
            configurable: true
        });
        WebApp.prototype.installStyleSheet = function (ssConfig) {
            if (typeof ssConfig !== "object") {
                ssConfig = { href: ssConfig };
            }
            ssConfig.href = UMD.resolvePath(ssConfig.href);
            var links = document.getElementsByTagName("link");
            // tslint:disable-next-line:prefer-for-of
            for (var index = 0; index < links.length; index++) {
                var element = links[index];
                var href = element.getAttribute("href");
                if (href === ssConfig.href) {
                    return;
                }
            }
            var ss = document.createElement("link");
            ss.rel = "stylesheet";
            ss.href = ssConfig.href;
            if (ssConfig.crossOrigin) {
                ss.crossOrigin = ssConfig.crossOrigin;
            }
            if (ssConfig.integrity) {
                ss.integrity = ssConfig.integrity;
            }
            document.head.appendChild(ss);
        };
        WebApp.prototype.installScript = function (location) {
            location = UMD.resolvePath(location);
            var links = document.getElementsByTagName("script");
            // tslint:disable-next-line:prefer-for-of
            for (var index = 0; index < links.length; index++) {
                var element = links[index];
                var href = element.getAttribute("src");
                if (href === location) {
                    return element.loaderPromise;
                }
            }
            var script = document.createElement("script");
            var p = new Promise(function (resolve, reject) {
                script.type = "text/javascript";
                script.src = location;
                var s = script;
                script.onload = s.onreadystatechange = function () {
                    if ((s.readyState && s.readyState !== "complete" && s.readyState !== "loaded")) {
                        return;
                    }
                    script.onload = s.onreadystatechange = null;
                    resolve();
                };
                document.body.appendChild(script);
            });
            script.loaderPromise = p;
            return p;
        };
        WebApp.prototype.updateDefaultStyle = function (textContent) {
            if (this.styleElement) {
                if (this.styleElement.textContent === textContent) {
                    return;
                }
            }
            var ss = document.createElement("style");
            ss.textContent = textContent;
            if (this.styleElement) {
                this.styleElement.remove();
            }
            document.head.appendChild(ss);
            this.styleElement = ss;
        };
        /**
         * Do not use this method
         */
        WebApp.prototype.syncUrl = function () {
            var _this = this;
            this.hashUpdater.run(function () {
                var currentUrl = new AtomUri_1.AtomUri(location.href);
                var sourceHash = _this.url.hash;
                var keyValues = [];
                var modified = false;
                for (var key in sourceHash) {
                    if (/^\_\$\_/.test(key)) {
                        continue;
                    }
                    if (sourceHash.hasOwnProperty(key)) {
                        var element = sourceHash[key];
                        var cv = currentUrl.hash[key];
                        if (element !== undefined) {
                            keyValues.push({ key: key, value: element });
                        }
                        if (cv === element) {
                            continue;
                        }
                        modified = true;
                    }
                }
                if (!modified) {
                    return;
                }
                var hash = keyValues.map(function (s) { return s.key + "=" + encodeURIComponent(s.value); }).join("&");
                location.hash = hash;
            });
        };
        WebApp.prototype.invokeReady = function () {
            var _this = this;
            if (document.readyState === "complete") {
                _super.prototype.invokeReady.call(this);
                return;
            }
            document.addEventListener("readystatechange", function (e) {
                _super.prototype.invokeReady.call(_this);
            });
        };
        return WebApp;
    }(App_1.App));
    exports.default = WebApp;
    // tslint:disable-next-line: only-arrow-functions
    (function () {
        if (typeof window.CustomEvent === "function") {
            return false;
        }
        function CustomEvent(event, params) {
            params = params || { bubbles: false, cancelable: false, detail: null };
            var evt = document.createEvent("CustomEvent");
            evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
            return evt;
        }
        window.CustomEvent = CustomEvent;
    })();
});
//# sourceMappingURL=WebApp.js.map