"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MenuGroupInstance = exports.UINavbarMenu = void 0;
var aurelia_router_1 = require("aurelia-router");
var aurelia_framework_1 = require("aurelia-framework");
var aurelia_templating_1 = require("aurelia-templating");
var auth_service_1 = require("../../security/auth-service");
var aurelia_binding_1 = require("aurelia-binding");
var config_1 = require("../../config");
var ui_internal_1 = require("../../utils/ui-internal");
var UINavbarMenu = exports.UINavbarMenu = (function () {
    function UINavbarMenu(authService, bindingEngine, config) {
        this.authService = authService;
        this.bindingEngine = bindingEngine;
        this.config = config;
        this.autoFold = false;
        this.viewBurger = true;
        this.keepCondensed = false;
        this.isCondensed = false;
        this.isHoverNavbar = false;
    }
    Object.defineProperty(UINavbarMenu.prototype, "authorizedTopRoutes", {
        get: function () {
            return this.newAuthorizedRoutes.filter(function (x) { return !x.isBottom; });
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UINavbarMenu.prototype, "authorizedBottomRoutes", {
        get: function () {
            return this.newAuthorizedRoutes.filter(function (x) { return x.isBottom; });
        },
        enumerable: false,
        configurable: true
    });
    UINavbarMenu.prototype.bind = function () {
        var _this = this;
        this.maxLevel = this.config.menuConfig.maxLevelVerticalNavbar;
        this.refreshRoutes();
        this.disposable = this.bindingEngine.propertyObserver(this.authService, 'currentUser').subscribe(function (newVal, oldVal) {
            _this.refreshRoutes();
        });
        ui_internal_1.UIInternal.subscribe(ui_internal_1.UIInternal.EVT_ROUTE_CHANGE, function () {
            _this.router.refreshNavigation();
            _this.refreshRoutes();
            _this.selectedCurrentRoute();
        });
        this.isCondensed = this.asCondensed();
        ui_internal_1.UIInternal.subscribe('router:navigation:complete', function () {
            _this.selectedCurrentRoute();
        });
        this.selectedCurrentRoute();
        ui_internal_1.UIInternal.subscribe(ui_internal_1.UIInternal.EVT_VIEWPORT_SMALL_DEVICE_ACTIVATED, function () {
            if (!_this.isCondensed) {
                _this.toggleMenu();
            }
        });
    };
    UINavbarMenu.prototype.selectedCurrentRoute = function () {
        var _this = this;
        this.newAuthorizedRoutes.forEach(function (menu) {
            var _a;
            var name;
            if (_this.router.currentInstruction.config.nav == true) {
                name = _this.router.currentInstruction.config.name;
            }
            else {
                name = _this.router.currentInstruction.config.settings.parentName;
            }
            if (menu.children == null || menu.children.length == 0) {
                if (_this.isSameRoute(menu, name)) {
                    (_a = _this.newAuthorizedRoutes.find(function (x) { return x.isSelected; })) === null || _a === void 0 ? void 0 : _a.setSelected(false);
                    menu.setSelected();
                }
            }
            else {
                menu.children.forEach(function (subMenu) {
                    var _a;
                    if (subMenu.children == null || subMenu.children.length == 0) {
                        if (_this.isSameRoute(subMenu, name)) {
                            (_a = _this.newAuthorizedRoutes.find(function (x) { return x.isSelected; })) === null || _a === void 0 ? void 0 : _a.setSelected(false);
                            subMenu.setSelected();
                            menu.viewChildren = true;
                        }
                    }
                    else {
                        subMenu.children.forEach(function (child) {
                            var _a;
                            if (_this.isSameRoute(child, name)) {
                                (_a = _this.newAuthorizedRoutes.find(function (x) { return x.isSelected; })) === null || _a === void 0 ? void 0 : _a.setSelected(false);
                                child.setSelected();
                                menu.viewChildren = true;
                                subMenu.viewChildren = true;
                            }
                        });
                    }
                });
            }
        });
    };
    UINavbarMenu.prototype.isSameRoute = function (menu, name) {
        return menu.route == this.router.currentInstruction.fragment || menu.route == "/" + this.router.currentInstruction.config.href || (name != null && menu.routeName == name);
    };
    UINavbarMenu.prototype.attached = function () {
        var _this = this;
        if (this.autoFold) {
            this.navbarMenu.onmouseenter = function () {
                _this.toggleClassHover(true);
            };
            this.navbarMenu.onmouseleave = function () {
                _this.toggleClassHover(false);
            };
            if (!this.isCondensed) {
                this.closeMenu();
            }
        }
    };
    UINavbarMenu.prototype.toggleClassHover = function (hover) {
        if (hover) {
            document.body.classList.add('ui-navbar-menu-hover');
            this.isHoverNavbar = true;
        }
        else {
            document.body.classList.remove('ui-navbar-menu-hover');
            this.isHoverNavbar = false;
        }
    };
    UINavbarMenu.prototype.detached = function () {
        this.disposable.dispose();
    };
    UINavbarMenu.prototype.refreshRoutes = function () {
        this.getAvailableRoutes();
    };
    UINavbarMenu.prototype.getAvailableRoutes = function () {
        var _a;
        var _this = this;
        if (this.router == null)
            throw "You must pass a router parameter when using ui-menu element";
        var authorizedRoutes = [];
        if (this.newAuthorizedRoutes == null) {
            this.newAuthorizedRoutes = [];
        }
        this.newAuthorizedRoutes.splice(0);
        authorizedRoutes = this.router.navigation;
        if (this.authService != null) {
            authorizedRoutes = authorizedRoutes.filter(function (r) { return _this.authService.checkRouteAccess(r.settings); });
        }
        var newAuthorizedRoutes = new Array();
        var routes = authorizedRoutes.filter(function (x) { return x.config.nav == true; }).groupBy('config.settings.group');
        routes.forEach(function (groupValue, groupKey) {
            var _a, _b, _c, _d, _e;
            var newGroup = new MenuGroupInstance(1, {
                group: groupKey,
                subGroup: null,
                title: groupKey,
                route: null,
                isDefault: false,
                classes: groupValue.length > 1 ? '' : groupValue[0].config.settings.classes,
            });
            if (groupKey != null) {
                var groupConfig_1 = _this.groupIcons.get(groupKey);
                var defaultSubGroup_1 = null;
                if (groupConfig_1 != null) {
                    newGroup.icon = groupConfig_1.icon;
                    newGroup.isBottom = groupConfig_1.isBottom;
                    newGroup.isDefault = groupConfig_1.isDefault;
                    newGroup.classes = groupConfig_1.classes;
                    defaultSubGroup_1 = groupConfig_1.defaultSubGroup;
                }
                var subGroup = groupValue.groupBy('config.settings.subGroup');
                if (subGroup.size == 1) {
                    subGroup.forEach(function (subValue, subKey) {
                        var _a;
                        if (subValue.length == 1 && (groupConfig_1 === null || groupConfig_1 === void 0 ? void 0 : groupConfig_1.alwaysGroup) !== true) {
                            newGroup.title = subValue[0].config.title;
                            newGroup.route = (_a = subValue[0].href) !== null && _a !== void 0 ? _a : subValue[0].relativeHref;
                            newGroup.routeName = subValue[0].config.name;
                            newGroup.isDefault = subValue[0].config.isDefault;
                            newGroup.classes = subValue[0].config.settings.classes;
                            newGroup.children = null;
                        }
                        else {
                            subValue.forEach(function (child) {
                                var _a;
                                var children = new MenuGroupInstance(2, {
                                    group: groupKey,
                                    subGroup: subKey,
                                    title: child.config.title,
                                    route: (_a = child.href) !== null && _a !== void 0 ? _a : child.relativeHref,
                                    routeName: child.config.name,
                                    isDefault: child.config.isDefault,
                                    classes: child.config.settings.classes
                                });
                                newGroup.children.push(children);
                            });
                        }
                    });
                }
                else {
                    subGroup.forEach(function (subValue, subKey) {
                        var newSubGroup = new MenuGroupInstance(2, {
                            group: groupKey,
                            subGroup: subKey,
                            title: subKey,
                            route: null,
                            isDefault: (defaultSubGroup_1 == subKey)
                        });
                        if (subValue != null) {
                            subValue.forEach(function (childrenValue, childrenKey) {
                                var _a;
                                var children = new MenuGroupInstance(3, {
                                    group: groupKey,
                                    subGroup: subKey,
                                    title: childrenValue.config.title,
                                    route: (_a = childrenValue.href) !== null && _a !== void 0 ? _a : childrenValue.relativeHref,
                                    routeName: childrenValue.config.name,
                                    isDefault: childrenValue.config.isDefault,
                                    classes: childrenValue.config.settings.classes
                                });
                                newSubGroup.children.push(children);
                            });
                        }
                        else {
                            console.log("subValue value: ", subValue);
                        }
                        newGroup.children.push(newSubGroup);
                    });
                }
            }
            else if (groupValue.length == 1) {
                newGroup.route = (_b = (_a = groupValue[0]) === null || _a === void 0 ? void 0 : _a.href) !== null && _b !== void 0 ? _b : (_c = groupValue[0]) === null || _c === void 0 ? void 0 : _c.relativeHref;
                newGroup.routeName = (_e = (_d = groupValue[0]) === null || _d === void 0 ? void 0 : _d.config) === null || _e === void 0 ? void 0 : _e.name;
            }
            newAuthorizedRoutes.push(newGroup);
        });
        (_a = this.newAuthorizedRoutes).push.apply(_a, __spreadArray([], __read(newAuthorizedRoutes), false));
    };
    UINavbarMenu.prototype.viewChildren = function (menu) {
        var _a, _b;
        if (menu.viewChildren) {
            var group = this.newAuthorizedRoutes.find(function (x) { return x.group == menu.group; });
            var subGroup = (_a = group === null || group === void 0 ? void 0 : group.children) === null || _a === void 0 ? void 0 : _a.find(function (x) { return x.subGroup == menu.subGroup; });
            if (group.viewChildren) {
                if (subGroup === null || subGroup === void 0 ? void 0 : subGroup.viewChildren) {
                    subGroup.viewChildren = false;
                }
                else {
                    group.viewChildren = false;
                }
            }
        }
        else {
            var group = this.newAuthorizedRoutes.find(function (x) { return x.viewChildren; });
            if (group != null) {
                var subViewChild = (_b = group.children) === null || _b === void 0 ? void 0 : _b.find(function (x) { return x.viewChildren; });
                if (subViewChild != null) {
                    subViewChild.viewChildren = false;
                }
                if (group.group != menu.group) {
                    group.viewChildren = false;
                }
            }
            menu.viewChildren = true;
        }
        if (this.onViewChildren) {
            this.onViewChildren(menu);
        }
    };
    UINavbarMenu.prototype.routeClick = function ($event, menu) {
        var _this = this;
        var _a, _b;
        if (this.asCondensed() && !this.keepCondensed) {
            this.openMenu();
        }
        if (menu.route != null) {
            if ($event.ctrlKey) {
                window.open(menu.route, "_blank");
            }
            else {
                var otherGroup = this.newAuthorizedRoutes.filter(function (x) { return x.route != menu.route && (x.children == null || x.children.every(function (c) { var _a; return c.route != menu.route && (c.children == null || ((_a = c.children) === null || _a === void 0 ? void 0 : _a.every(function (sc) { return sc.route != menu.route; }))); })); });
                otherGroup.forEach(function (x) {
                    _this.changeViewChildren(x, false);
                });
                this.router.navigate(menu.route);
            }
        }
        if (this.onSelected && menu.isSelected && ((_a = menu.children) === null || _a === void 0 ? void 0 : _a.length) == 0) {
            this.onSelected(menu);
        }
        if (((_b = menu.children) === null || _b === void 0 ? void 0 : _b.length) > 0) {
            this.viewChildren(menu);
        }
    };
    UINavbarMenu.prototype.changeViewChildren = function (menu, viewChildren) {
        var _this = this;
        var _a;
        (_a = menu.children) === null || _a === void 0 ? void 0 : _a.forEach(function (children) { return _this.changeViewChildren(children, viewChildren); });
        menu.viewChildren = viewChildren;
    };
    UINavbarMenu.prototype.menuIsSelected = function (menu) {
        var _this = this;
        var _a;
        if (((_a = menu.children) === null || _a === void 0 ? void 0 : _a.length) > 0) {
            menu.children.forEach(function (x) {
                _this.menuIsSelected(x);
            });
            return false;
        }
        else {
            return menu.isSelected;
        }
    };
    UINavbarMenu.prototype.toggleMenu = function () {
        if (this.asCondensed()) {
            this.openMenu();
        }
        else {
            this.closeMenu();
        }
    };
    UINavbarMenu.prototype.asCondensed = function () {
        return document.body.classList.contains("condensed");
    };
    UINavbarMenu.prototype.openMenu = function () {
        document.body.classList.remove("condensed");
        this.isCondensed = !this.isCondensed;
    };
    UINavbarMenu.prototype.closeMenu = function () {
        document.body.classList.add("condensed");
        this.closeCollapsible();
        this.isCondensed = !this.isCondensed;
    };
    UINavbarMenu.prototype.closeCollapsible = function () {
        var route = this.newAuthorizedRoutes.find(function (x) { return x.isSelected; });
        if (route != null) {
            route.viewChildren = false;
        }
    };
    __decorate([
        aurelia_templating_1.bindable,
        __metadata("design:type", aurelia_router_1.Router)
    ], UINavbarMenu.prototype, "router", void 0);
    __decorate([
        aurelia_templating_1.bindable,
        __metadata("design:type", Map)
    ], UINavbarMenu.prototype, "groupIcons", void 0);
    __decorate([
        (0, aurelia_templating_1.bindable)({ defaultBindingMode: aurelia_binding_1.bindingMode.oneTime }),
        __metadata("design:type", Boolean)
    ], UINavbarMenu.prototype, "autoFold", void 0);
    __decorate([
        aurelia_templating_1.bindable,
        __metadata("design:type", Boolean)
    ], UINavbarMenu.prototype, "viewBurger", void 0);
    __decorate([
        aurelia_templating_1.bindable,
        __metadata("design:type", Object)
    ], UINavbarMenu.prototype, "keepCondensed", void 0);
    __decorate([
        aurelia_templating_1.bindable,
        __metadata("design:type", Function)
    ], UINavbarMenu.prototype, "onSelected", void 0);
    __decorate([
        aurelia_templating_1.bindable,
        __metadata("design:type", Function)
    ], UINavbarMenu.prototype, "onViewChildren", void 0);
    __decorate([
        (0, aurelia_templating_1.bindable)({ defaultBindingMode: aurelia_binding_1.bindingMode.twoWay }),
        __metadata("design:type", Boolean)
    ], UINavbarMenu.prototype, "isCondensed", void 0);
    __decorate([
        (0, aurelia_templating_1.bindable)({ defaultBindingMode: aurelia_binding_1.bindingMode.twoWay }),
        __metadata("design:type", Array)
    ], UINavbarMenu.prototype, "newAuthorizedRoutes", void 0);
    __decorate([
        (0, aurelia_binding_1.computedFrom)("newAuthorizedRoutes.length"),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], UINavbarMenu.prototype, "authorizedTopRoutes", null);
    __decorate([
        (0, aurelia_binding_1.computedFrom)("newAuthorizedRoutes.length"),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], UINavbarMenu.prototype, "authorizedBottomRoutes", null);
    UINavbarMenu = __decorate([
        (0, aurelia_templating_1.customElement)("ui-navbar-menu"),
        (0, aurelia_framework_1.singleton)(),
        aurelia_framework_1.autoinject,
        __metadata("design:paramtypes", [auth_service_1.AuthService, aurelia_framework_1.BindingEngine, config_1.Config])
    ], UINavbarMenu);
    return UINavbarMenu;
}());
var MenuGroupInstance = (function () {
    function MenuGroupInstance(level, data) {
        this.children = [];
        this._isSelected = false;
        this.viewChildren = false;
        this.isBottom = false;
        this.isDefault = false;
        Object.assign(this, data);
        this._level = level;
    }
    Object.defineProperty(MenuGroupInstance.prototype, "isSelected", {
        get: function () {
            var _a;
            if (((_a = this.children) === null || _a === void 0 ? void 0 : _a.length) > 0) {
                return this.children.find(function (child) { return child.isSelected; }) != null;
            }
            return this._isSelected;
        },
        enumerable: false,
        configurable: true
    });
    MenuGroupInstance.prototype.setSelected = function (isSelected) {
        var _a, _b;
        if (isSelected === void 0) { isSelected = true; }
        if (((_a = this.children) === null || _a === void 0 ? void 0 : _a.length) > 0) {
            if (this.isSelected && isSelected == false) {
                (_b = this.children.find(function (x) { return x.isSelected; })) === null || _b === void 0 ? void 0 : _b.setSelected(false);
            }
        }
        else {
            this._isSelected = isSelected;
        }
        if (isSelected == true) {
            this.viewChildren = true;
        }
    };
    Object.defineProperty(MenuGroupInstance.prototype, "level", {
        get: function () {
            return this._level;
        },
        enumerable: false,
        configurable: true
    });
    MenuGroupInstance.prototype.hasChildren = function (maxLevel) {
        return this.children != null && this.children.length > 0 && this.level != maxLevel;
    };
    return MenuGroupInstance;
}());
exports.MenuGroupInstance = MenuGroupInstance;

//# sourceMappingURL=ui-navbar-menu.js.map
