/*
 * Application Insights JavaScript SDK - Debug Plugin, 3.4.0-nightlybeta3.2602-27
 * Copyright (c) Microsoft and contributors. All rights reserved.
 */


import { arrIndexOf } from "@microsoft/applicationinsights-core-js";
import { _DYN_APPEND_CHILD, _DYN_CHILDREN, _DYN_CLASS_NAME, _DYN_CREATE_ELEMENT, _DYN_FILTER_LIST, _DYN_FOCUS, _DYN_INNER_TEXT, _DYN_ONCLICK, _DYN_ONKEYUP, _DYN_PUSH, _DYN_REMOVE_CHILD, _DYN_SET_ATTRIBUTE, _DYN_STOP_PROPAGATION } from "../__DynamicConstants";
var FilterList = /** @class */ (function () {
    function FilterList(parent, trackers, toggleCb) {
        var _this = this;
        this.children = [];
        this.getCurrentFilter = function () {
            return _this[_DYN_FILTER_LIST /* @min:%2efilterList */];
        };
        var _self = this;
        var active = false;
        trackers = trackers.concat("other");
        _self[_DYN_FILTER_LIST /* @min:%2efilterList */] = [];
        _self.el = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("div");
        _self.el[_DYN_CLASS_NAME /* @min:%2eclassName */] = "filterlist";
        var inputEl = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("div");
        inputEl[_DYN_CLASS_NAME /* @min:%2eclassName */] = "filterlist-input";
        inputEl[_DYN_INNER_TEXT /* @min:%2einnerText */] = "filter nodes...";
        _self.el[_DYN_APPEND_CHILD /* @min:%2eappendChild */](inputEl);
        var dropdownMenuEl = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("div");
        dropdownMenuEl[_DYN_CLASS_NAME /* @min:%2eclassName */] = "filterlist-dropdown";
        _self.el[_DYN_APPEND_CHILD /* @min:%2eappendChild */](dropdownMenuEl);
        dropdownMenuEl[_DYN_ONKEYUP /* @min:%2eonkeyup */] = function (evt) {
            if (active && evt.keyCode === 27) {
                evt[_DYN_STOP_PROPAGATION /* @min:%2estopPropagation */]();
                _hideDropdown();
            }
        };
        var _loop_1 = function (t) {
            var chEl = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("div");
            chEl[_DYN_SET_ATTRIBUTE /* @min:%2esetAttribute */]("tabindex", "0");
            chEl[_DYN_SET_ATTRIBUTE /* @min:%2esetAttribute */]("filter-type", t);
            chEl[_DYN_CLASS_NAME /* @min:%2eclassName */] = "filterlist-toggle";
            var checkbox = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("div");
            checkbox[_DYN_CLASS_NAME /* @min:%2eclassName */] = "checkbox on";
            var label = document[_DYN_CREATE_ELEMENT /* @min:%2ecreateElement */]("span");
            label[_DYN_CLASS_NAME /* @min:%2eclassName */] = "label";
            label[_DYN_INNER_TEXT /* @min:%2einnerText */] = t;
            chEl[_DYN_APPEND_CHILD /* @min:%2eappendChild */](checkbox);
            chEl[_DYN_APPEND_CHILD /* @min:%2eappendChild */](label);
            chEl[_DYN_ONKEYUP /* @min:%2eonkeyup */] = function (evt) {
                if (active && evt.keyCode === 27) {
                    evt[_DYN_STOP_PROPAGATION /* @min:%2estopPropagation */]();
                    _hideDropdown();
                }
            };
            chEl[_DYN_ONCLICK /* @min:%2eonclick */] = function (evt) {
                evt[_DYN_STOP_PROPAGATION /* @min:%2estopPropagation */]();
                chEl[_DYN_FOCUS /* @min:%2efocus */]();
                if (checkbox[_DYN_CLASS_NAME /* @min:%2eclassName */] === "checkbox off") {
                    if (evt.shiftKey) {
                        _self[_DYN_FILTER_LIST /* @min:%2efilterList */] = trackers.concat();
                        for (var _i = 0, _a = _self[_DYN_CHILDREN /* @min:%2echildren */]; _i < _a.length; _i++) {
                            var ch = _a[_i];
                            ch.childNodes[0][_DYN_CLASS_NAME /* @min:%2eclassName */] = "checkbox off";
                        }
                    }
                    _self[_DYN_FILTER_LIST /* @min:%2efilterList */].splice(arrIndexOf(_self[_DYN_FILTER_LIST /* @min:%2efilterList */], t), 1);
                    checkbox[_DYN_CLASS_NAME /* @min:%2eclassName */] = "checkbox on";
                }
                else {
                    if (evt.shiftKey) {
                        _self[_DYN_FILTER_LIST /* @min:%2efilterList */] = [];
                        for (var _b = 0, _c = _self[_DYN_CHILDREN /* @min:%2echildren */]; _b < _c.length; _b++) {
                            var ch = _c[_b];
                            ch.childNodes[0][_DYN_CLASS_NAME /* @min:%2eclassName */] = "checkbox on";
                        }
                    }
                    _self.filterList[_DYN_PUSH /* @min:%2epush */](t);
                    checkbox[_DYN_CLASS_NAME /* @min:%2eclassName */] = "checkbox off";
                }
                toggleCb();
            };
            _self.children[_DYN_PUSH /* @min:%2epush */](chEl);
        };
        for (var _i = 0, trackers_1 = trackers; _i < trackers_1.length; _i++) {
            var t = trackers_1[_i];
            _loop_1(t);
        }
        function _hideDropdown() {
            active = false;
            for (var _i = 0, _a = _self[_DYN_CHILDREN /* @min:%2echildren */]; _i < _a.length; _i++) {
                var ch = _a[_i];
                dropdownMenuEl[_DYN_REMOVE_CHILD /* @min:%2eremoveChild */](ch);
            }
        }
        function _showDropdown() {
            active = true;
            for (var _i = 0, _a = _self[_DYN_CHILDREN /* @min:%2echildren */]; _i < _a.length; _i++) {
                var ch = _a[_i];
                dropdownMenuEl[_DYN_APPEND_CHILD /* @min:%2eappendChild */](ch);
            }
            dropdownMenuEl[_DYN_FOCUS /* @min:%2efocus */]();
        }
        _self.el[_DYN_ONKEYUP /* @min:%2eonkeyup */] = function (evt) {
            if (active && evt.keyCode === 27) {
                evt[_DYN_STOP_PROPAGATION /* @min:%2estopPropagation */]();
                _hideDropdown();
            }
        };
        _self.el[_DYN_ONCLICK /* @min:%2eonclick */] = function () {
            if (!active) {
                _showDropdown();
            }
            else {
                _hideDropdown();
            }
        };
        parent[_DYN_APPEND_CHILD /* @min:%2eappendChild */](_self.el);
    }
    return FilterList;
}());
export { FilterList };
//# sourceMappingURL=filterList.js.map