/*
 * Application Insights JavaScript SDK - Debug Plugin, 3.4.0-nightlybeta3.2602-27
 * Copyright (c) Microsoft and contributors. All rights reserved.
 */
import dynamicProto from "@microsoft/dynamicproto-js";
import { arrForEach, arrIndexOf } from "@microsoft/applicationinsights-core-js";
import { scheduleTimeout } from "@nevware21/ts-utils";
import { _DYN_APPEND_CHILD, _DYN_CLASS_NAME, _DYN_CREATE_ELEMENT, _DYN_EXCLUDE_KEYS, _DYN_INCLUDE_FUNCTIONS, _DYN_INNER_TEXT, _DYN_IS_DISPLAYED, _DYN_LENGTH, _DYN_NEW_LOG_ENTRY, _DYN_ONCLICK, _DYN_ONKEYUP, _DYN_ONTOUCHEND, _DYN_PARENT_ELEMENT, _DYN_POINTER_EVENTS, _DYN_PUSH, _DYN_REMOVE_CHILD, _DYN_RENDER, _DYN_SET_ATTRIBUTE, _DYN_SET_TEXT_FILTER } from "../__DynamicConstants";
import { LogEntry } from "./LogEntry";
import { FilterList } from "./filterList";
import { copySelectedTree } from "./helpers";
import { tempStyle } from "./styleNodeSrc";
var Dashboard = /** @class */ (function () {
    function Dashboard(config) {
        var _this = this;
        var msgTracker = [];
        var textFilter = "";
        /**
         * the root element of the logger
         */
        var rootElement;
        /**
         * the style that is only on the webpage when the log is active
         */
        var tempStyleEl;
        var filterList;
        var loggerEl;
        dynamicProto(Dashboard, this, function (_self) {
            var prefix = config.prefix;
            var trackers = config.trackers;
            var rootEl = rootElement = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("div");
            rootEl[_DYN_CLASS_NAME /* @min:%2eclassName */] = "".concat(prefix, "-dbg-lgr-dashboard");
            tempStyleEl = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("style");
            tempStyleEl.innerHTML = tempStyle(prefix);
            // TODO: research more accessibility (aria)
            rootEl.style.position = "fixed";
            rootEl.style.width = "100vw";
            rootEl.style.height = "100vh";
            rootEl.style.backgroundColor = "#ffffff";
            rootEl.style.opacity = "1";
            rootEl.style[_DYN_POINTER_EVENTS /* @min:%2epointerEvents */] = "auto";
            rootEl.style.top = "-100%";
            rootEl.style.transition = ".2s top cubic-bezier(0.87, 0, 0.13, 1)";
            var logHeading = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("h1");
            logHeading.textContent = "dashboard";
            logHeading.style.fontFamily = "monospace";
            logHeading.style.textAlign = "center";
            rootEl[_DYN_APPEND_CHILD /* @min:%2eappendChild */](logHeading);
            _createLogger(rootEl, prefix, trackers);
            _self.getElm = function () {
                return rootEl;
            };
            _self[_DYN_IS_DISPLAYED /* @min:%2eisDisplayed */] = function () {
                return !!(rootEl[_DYN_PARENT_ELEMENT /* @min:%2eparentElement */] || rootEl.parentNode);
            };
            _self.getTextFilter = function () {
                return textFilter;
            };
            _self[_DYN_SET_TEXT_FILTER /* @min:%2esetTextFilter */] = function (value) {
                if (value !== textFilter) {
                    textFilter = value;
                    _self[_DYN_RENDER /* @min:%2erender */]();
                }
            };
            _self.getTrackers = function () {
                return trackers;
            };
            _self[_DYN_NEW_LOG_ENTRY /* @min:%2enewLogEntry */] = function (target, tm, key, level, kind, keep) {
                var _self = _this;
                msgTracker[_DYN_PUSH /* @min:%2epush */](new LogEntry(target, tm, key, level, kind, keep));
                if (msgTracker[_DYN_LENGTH /* @min:%2elength */] > config.maxMessages) {
                    var lp = 0;
                    while (lp < msgTracker[_DYN_LENGTH /* @min:%2elength */]) {
                        var entry = msgTracker[lp];
                        if (!entry.isKeep()) {
                            var el = entry.getEl();
                            if (el && el[_DYN_PARENT_ELEMENT /* @min:%2eparentElement */]) {
                                el[_DYN_PARENT_ELEMENT /* @min:%2eparentElement */][_DYN_REMOVE_CHILD /* @min:%2eremoveChild */](el);
                            }
                            msgTracker.splice(lp, 1);
                            break;
                        }
                        lp++;
                    }
                }
                _self[_DYN_RENDER /* @min:%2erender */]();
            };
            _self.show = function () {
                if (!_this[_DYN_IS_DISPLAYED /* @min:%2eisDisplayed */]()) {
                    document.body[_DYN_APPEND_CHILD /* @min:%2eappendChild */](rootEl);
                    document.head[_DYN_APPEND_CHILD /* @min:%2eappendChild */](tempStyleEl);
                    rootEl.style.top = "0%";
                    rootEl.style[_DYN_POINTER_EVENTS /* @min:%2epointerEvents */] = "auto";
                    _self[_DYN_RENDER /* @min:%2erender */]();
                }
            };
            _self.hide = function () {
                if (_self[_DYN_IS_DISPLAYED /* @min:%2eisDisplayed */]()) {
                    rootEl.style.top = "-100%";
                    rootEl.style[_DYN_POINTER_EVENTS /* @min:%2epointerEvents */] = "none";
                    document.head[_DYN_REMOVE_CHILD /* @min:%2eremoveChild */](tempStyleEl);
                    document.body[_DYN_REMOVE_CHILD /* @min:%2eremoveChild */](rootEl);
                }
            };
            _self[_DYN_RENDER /* @min:%2erender */] = function () {
                if (_self[_DYN_IS_DISPLAYED /* @min:%2eisDisplayed */]()) {
                    var excludedTypes_1 = filterList.getCurrentFilter();
                    arrForEach(msgTracker, function (entry) {
                        var el = entry.getEl();
                        if (el && el[_DYN_PARENT_ELEMENT /* @min:%2eparentElement */]) {
                            el[_DYN_PARENT_ELEMENT /* @min:%2eparentElement */][_DYN_REMOVE_CHILD /* @min:%2eremoveChild */](el);
                        }
                        if (!entry.isMatch(textFilter, config[_DYN_EXCLUDE_KEYS /* @min:%2eexcludeKeys */], config[_DYN_INCLUDE_FUNCTIONS /* @min:%2eincludeFunctions */])) {
                            return;
                        }
                        var type = entry.getKind();
                        var allowOther = arrIndexOf(excludedTypes_1, "other") === -1; // Other types are not excluded
                        if (arrIndexOf(trackers, type) === -1 && !allowOther) {
                            // Not a tracked type and we are not allowing other types
                            return;
                        }
                        else if (arrIndexOf(excludedTypes_1, type) !== -1) {
                            // This type is explicitly excluded
                            return;
                        }
                        var newEl = entry.render(textFilter, config[_DYN_EXCLUDE_KEYS /* @min:%2eexcludeKeys */], config[_DYN_INCLUDE_FUNCTIONS /* @min:%2eincludeFunctions */]);
                        if (newEl) {
                            loggerEl[_DYN_APPEND_CHILD /* @min:%2eappendChild */](newEl);
                        }
                    });
                }
            };
            function clearEvents() {
                var newEvents = [];
                arrForEach(msgTracker, function (entry) {
                    if (entry.isKeep()) {
                        newEvents[_DYN_PUSH /* @min:%2epush */](entry);
                    }
                    else {
                        var el = entry.getEl();
                        if (el && el[_DYN_PARENT_ELEMENT /* @min:%2eparentElement */]) {
                            el[_DYN_PARENT_ELEMENT /* @min:%2eparentElement */][_DYN_REMOVE_CHILD /* @min:%2eremoveChild */](el);
                        }
                    }
                });
                msgTracker = newEvents;
                _self[_DYN_RENDER /* @min:%2erender */]();
            }
            function closeDashboard() {
                _self.hide();
            }
            function _createLogger(hostDiv, prefix, trackers) {
                loggerEl = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("div");
                loggerEl[_DYN_CLASS_NAME /* @min:%2eclassName */] = "".concat(prefix, "-dbg-lgr");
                var controlDiv = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("div");
                controlDiv[_DYN_CLASS_NAME /* @min:%2eclassName */] = "controls";
                var textFilterInput = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("input");
                textFilterInput[_DYN_CLASS_NAME /* @min:%2eclassName */] = "text-filter-input";
                textFilterInput[_DYN_SET_ATTRIBUTE /* @min:%2esetAttribute */]("placeholder", "filter text");
                textFilterInput.onchange = function (evt) {
                    _self[_DYN_SET_TEXT_FILTER /* @min:%2esetTextFilter */](textFilterInput.value);
                };
                textFilterInput.onblur = function (evt) {
                    _self[_DYN_SET_TEXT_FILTER /* @min:%2esetTextFilter */](textFilterInput.value);
                };
                textFilterInput[_DYN_ONKEYUP /* @min:%2eonkeyup */] = function (evt) {
                    keyupTimer && keyupTimer.cancel();
                    keyupTimer = null;
                    var newValue = textFilterInput.value;
                    if (newValue !== _self.getTextFilter()) {
                        keyupTimer = scheduleTimeout(function () {
                            keyupTimer = null;
                            _self[_DYN_SET_TEXT_FILTER /* @min:%2esetTextFilter */](textFilterInput.value);
                        }, 200);
                    }
                };
                controlDiv[_DYN_APPEND_CHILD /* @min:%2eappendChild */](textFilterInput);
                var copyButton = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("button");
                copyButton[_DYN_INNER_TEXT /* @min:%2einnerText */] = "copy current node";
                copyButton[_DYN_CLASS_NAME /* @min:%2eclassName */] = "btn-secondary";
                copyButton[_DYN_ONCLICK /* @min:%2eonclick */] = copySelectedTree;
                copyButton[_DYN_ONTOUCHEND /* @min:%2eontouchend */] = copySelectedTree;
                var keyupTimer = null;
                filterList = new FilterList(controlDiv, trackers.slice(0), function () { return _self[_DYN_RENDER /* @min:%2erender */](); });
                controlDiv[_DYN_APPEND_CHILD /* @min:%2eappendChild */](copyButton);
                var clearEventsBtn = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("button");
                clearEventsBtn[_DYN_INNER_TEXT /* @min:%2einnerText */] = "clear events";
                clearEventsBtn[_DYN_CLASS_NAME /* @min:%2eclassName */] = "btn-secondary";
                clearEventsBtn[_DYN_ONCLICK /* @min:%2eonclick */] = clearEvents;
                clearEventsBtn[_DYN_ONTOUCHEND /* @min:%2eontouchend */] = clearEvents;
                controlDiv[_DYN_APPEND_CHILD /* @min:%2eappendChild */](clearEventsBtn);
                var closeEventsBtn = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("button");
                closeEventsBtn.id = "close-dashboard";
                closeEventsBtn[_DYN_INNER_TEXT /* @min:%2einnerText */] = "close dashboard";
                closeEventsBtn[_DYN_CLASS_NAME /* @min:%2eclassName */] = "btn-primary";
                closeEventsBtn[_DYN_ONCLICK /* @min:%2eonclick */] = closeDashboard;
                closeEventsBtn[_DYN_ONTOUCHEND /* @min:%2eontouchend */] = closeDashboard;
                controlDiv[_DYN_APPEND_CHILD /* @min:%2eappendChild */](closeEventsBtn);
                hostDiv[_DYN_APPEND_CHILD /* @min:%2eappendChild */](controlDiv);
                hostDiv[_DYN_APPEND_CHILD /* @min:%2eappendChild */](loggerEl);
            }
        });
    }
// Removed Stub for Dashboard.prototype.getElm.
// Removed Stub for Dashboard.prototype.isDisplayed.
// Removed Stub for Dashboard.prototype.getTextFilter.
// Removed Stub for Dashboard.prototype.setTextFilter.
// Removed Stub for Dashboard.prototype.getTrackers.
// Removed Stub for Dashboard.prototype.newLogEntry.
// Removed Stub for Dashboard.prototype.render.
// Removed Stub for Dashboard.prototype.show.
// Removed Stub for Dashboard.prototype.hide.
    // This is a workaround for an IE bug when using dynamicProto() with classes that don't have any
    // non-dynamic functions or static properties/functions when using uglify-js to minify the resulting code.
    Dashboard.__ieDyn=1;

    return Dashboard;
}());
export { Dashboard };
//# sourceMappingURL=Dashboard.js.map