/*
 * Application Insights JavaScript SDK - Debug Plugin, 3.3.12-nightly3.2602-20
 * Copyright (c) Microsoft and contributors. All rights reserved.
 */


import { scheduleTimeout } from "@nevware21/ts-utils";
import { _DYN_APPEND_CHILD, _DYN_CHILDREN, _DYN_CLASS_NAME, _DYN_CREATE_ELEMENT, _DYN_INNER_TEXT, _DYN_ONCLICK, _DYN_PARENT_ELEMENT, _DYN_PREVENT_DEFAULT, _DYN_PUSH, _DYN_REMOVE_CHILD, _DYN_RENDER, _DYN_SET_ATTRIBUTE, _DYN_SHOW_CHILDREN } from "../__DynamicConstants";
var DebugBinParent = /** @class */ (function () {
    function DebugBinParent(parent, children, bottomDistance, prefix) {
        this.parent = parent;
        this.children = children;
        this.showChildren = false;
        this.value = 0;
        var _self = this;
        _self.el = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("div");
        _self.el[_DYN_SET_ATTRIBUTE /* @min:%2esetAttribute */]("tabindex", "0");
        _self.el[_DYN_CLASS_NAME /* @min:%2eclassName */] = "".concat(prefix, "-debug-bin-parent");
        _self.el.style.bottom = "".concat(20 + bottomDistance, "px");
        _self.elSpan = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("span");
        _self.elSpan[_DYN_INNER_TEXT /* @min:%2einnerText */] = "".concat(_self.value);
        _self.elSpan[_DYN_SET_ATTRIBUTE /* @min:%2esetAttribute */]("data-name", "AppInsights");
        _self.el[_DYN_APPEND_CHILD /* @min:%2eappendChild */](_self.elSpan);
        _self.el[_DYN_ONCLICK /* @min:%2eonclick */] = function () {
            _self[_DYN_SHOW_CHILDREN /* @min:%2eshowChildren */] = !_self[_DYN_SHOW_CHILDREN /* @min:%2eshowChildren */];
            _self.el[_DYN_CLASS_NAME /* @min:%2eclassName */] = (_self[_DYN_SHOW_CHILDREN /* @min:%2eshowChildren */]) ? "".concat(prefix, "-debug-bin-parent active") : "".concat(prefix, "-debug-bin-parent");
            _self[_DYN_RENDER /* @min:%2erender */]();
        };
        _self.el.onkeydown = function (evt) {
            if (evt.which === 13 || evt.which === 32) {
                evt[_DYN_PREVENT_DEFAULT /* @min:%2epreventDefault */]();
                evt.target.click();
            }
        };
        _self[_DYN_RENDER /* @min:%2erender */]();
    }
    DebugBinParent.prototype.addClassToEl = function (name) {
        var _self = this;
        if (_self.el[_DYN_CLASS_NAME /* @min:%2eclassName */].indexOf(name) === -1) {
            _self.el[_DYN_CLASS_NAME /* @min:%2eclassName */] += " ".concat(name);
        }
    };
    DebugBinParent.prototype.addChild = function (bin) {
        this.children[_DYN_PUSH /* @min:%2epush */](bin);
    };
    DebugBinParent.prototype.addButton = function (handler, label) {
        var btn = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("button");
        btn[_DYN_INNER_TEXT /* @min:%2einnerText */] = label;
        btn[_DYN_ONCLICK /* @min:%2eonclick */] = handler;
        this.addChild(btn);
    };
    DebugBinParent.prototype.increment = function () {
        var _self = this;
        _self.elSpan[_DYN_INNER_TEXT /* @min:%2einnerText */] = "".concat(++_self.value);
    };
    DebugBinParent.prototype.render = function () {
        var _self = this;
        if (!_self.el[_DYN_PARENT_ELEMENT /* @min:%2eparentElement */]) {
            _self.parent[_DYN_APPEND_CHILD /* @min:%2eappendChild */](_self.el);
        }
        for (var _i = 0, _a = _self[_DYN_CHILDREN /* @min:%2echildren */]; _i < _a.length; _i++) {
            var ch = _a[_i];
            if (ch instanceof DebugBin) {
                if (ch.el.parentElement !== _self.el && _self[_DYN_SHOW_CHILDREN /* @min:%2eshowChildren */]) {
                    _self.el[_DYN_APPEND_CHILD /* @min:%2eappendChild */](ch.el);
                }
                else if (ch.el.parentElement === _self.el && !_self[_DYN_SHOW_CHILDREN /* @min:%2eshowChildren */]) {
                    _self.el[_DYN_REMOVE_CHILD /* @min:%2eremoveChild */](ch.el);
                }
            }
            else {
                if (_self[_DYN_SHOW_CHILDREN /* @min:%2eshowChildren */]) {
                    _self.el[_DYN_APPEND_CHILD /* @min:%2eappendChild */](ch);
                }
                else {
                    _self.el[_DYN_REMOVE_CHILD /* @min:%2eremoveChild */](ch);
                }
            }
        }
    };
    return DebugBinParent;
}());
export { DebugBinParent };
var DebugBin = /** @class */ (function () {
    function DebugBin(name, value, parent, bottomDistance, backgroundColor) {
        this.name = name;
        this.value = value;
        this.parent = parent;
        var _self = this;
        parent.addChild(_self);
        _self.el = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("div");
        _self.elName = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("span");
        _self.elName[_DYN_INNER_TEXT /* @min:%2einnerText */] = "".concat(name, ": ");
        _self.elName[_DYN_CLASS_NAME /* @min:%2eclassName */] = "el-name";
        _self.el[_DYN_APPEND_CHILD /* @min:%2eappendChild */](_self.elName);
        _self.elValue = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("span");
        _self.elValue[_DYN_INNER_TEXT /* @min:%2einnerText */] = "".concat(value);
        _self.elValue[_DYN_CLASS_NAME /* @min:%2eclassName */] = "el-value";
        _self.el[_DYN_APPEND_CHILD /* @min:%2eappendChild */](_self.elValue);
    }
    DebugBin.prototype.increment = function () {
        var _self = this;
        _self.elValue[_DYN_INNER_TEXT /* @min:%2einnerText */] = "".concat(++_self.value);
        _self.parent.increment();
        _self.elValue[_DYN_CLASS_NAME /* @min:%2eclassName */] = "el-value incremented";
        scheduleTimeout(function () { return _self.elValue[_DYN_CLASS_NAME /* @min:%2eclassName */] = "el-value"; }, 1);
    };
    return DebugBin;
}());
export { DebugBin };
//# sourceMappingURL=debugBins.js.map