"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UITabRouterView = void 0;
var aurelia_i18n_1 = require("aurelia-i18n");
var aurelia_event_aggregator_1 = require("aurelia-event-aggregator");
var aurelia_router_1 = require("aurelia-router");
var aurelia_framework_1 = require("aurelia-framework");
var common_model_1 = require("../../../model/common-model");
var UITabRouterView = exports.UITabRouterView = (function () {
    function UITabRouterView(taskQueue, router, ea, i18n) {
        var _this = this;
        this.taskQueue = taskQueue;
        this.router = router;
        this.ea = ea;
        this.i18n = i18n;
        this.name = "default";
        this.tooltip = true;
        this.routesTabs = [];
        this.routesTabsStorage = [];
        this.show = true;
        this.restoreRoutesTabs();
        ea.subscribe("router:navigation:success", function (event) {
            if ((event === null || event === void 0 ? void 0 : event.instruction) != null) {
                _this.run(event.instruction);
            }
        });
    }
    Object.defineProperty(UITabRouterView.prototype, "routesTabDisplay", {
        get: function () {
            return this.routesTabs;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(UITabRouterView.prototype, "classes", {
        get: function () {
            if (this.routesTabs != null && this.routesTabs.length == 0) {
                return 'ui-tab-router-view-empty';
            }
            return '';
        },
        enumerable: false,
        configurable: true
    });
    UITabRouterView.prototype.attached = function () {
        this.routesTabsStorage = this.clearUnknowRoutes(this.routesTabsStorage);
    };
    UITabRouterView.prototype.run = function (navigationInstruction) {
        var _a, _b;
        if ((navigationInstruction === null || navigationInstruction === void 0 ? void 0 : navigationInstruction.config) == null || ((_b = (_a = navigationInstruction.config) === null || _a === void 0 ? void 0 : _a.settings) === null || _b === void 0 ? void 0 : _b.tab) == false) {
            this.show = false;
            return;
        }
        if (navigationInstruction.config.route.indexOf('*childRoute') > -1) {
            this.manageChildRoute(navigationInstruction);
            return;
        }
        if (navigationInstruction.config.name == 'callback' || navigationInstruction.config.name == 'login')
            return;
        this.show = true;
        var route = this.extractRoute(navigationInstruction);
        if (route == null) {
            this.routesTabs = [];
            throw ('UITabRouterView :: route is null !');
        }
        var resource = this.extractResource(route);
        this.setCurrentRouteTabsGroup(resource);
        var routeUniqueTitle = this.extractRouteUniqueTitle(navigationInstruction);
        if (routeUniqueTitle.toLowerCase() == 'login') {
            this.show = false;
            return;
        }
        this.routesTabsStorage.forEach(function (rts) { return rts.routesTabs.forEach(function (rt) { return rt.active = false; }); });
        var rt = this.getRouteTab(navigationInstruction, resource);
        rt.active = true;
        rt.name = routeUniqueTitle;
        rt.childRoute = null;
        this.routesTabs = this.routesTabs.sort(function (a, b) { return a.first == b.first ? 0 : a.first ? -1 : 1; });
        this.storeRoutesTabs(resource, !rt.first);
    };
    UITabRouterView.prototype.manageChildRoute = function (navigationInstruction) {
        if (navigationInstruction.config.route.indexOf('*childRoute') == -1)
            return;
        var route = this.extractRoute(navigationInstruction);
        var childRouteIndex = route.indexOf('/*childRoute');
        route = route.slice(0, childRouteIndex);
        var resource = this.extractResource(route);
        this.setCurrentRouteTabsGroup(resource);
        var fragment = navigationInstruction.fragment;
        childRouteIndex = fragment.indexOf('/' + navigationInstruction.params.childRoute);
        fragment = fragment.slice(0, childRouteIndex);
        var rt = this.routesTabs.find(function (r) { return r.fragment == fragment && r.resource == resource; });
        if (rt != null) {
            this.routesTabsStorage.forEach(function (rts) { return rts.routesTabs.forEach(function (rt) { return rt.active = false; }); });
            rt.active = true;
            rt.childRoute = navigationInstruction.fragment.slice(1) + (navigationInstruction.queryString != null ? '?' + navigationInstruction.queryString : '');
            this.storeRoutesTabs(resource, !rt.first);
        }
    };
    UITabRouterView.prototype.setCurrentRouteTabsGroup = function (resource) {
        if (this.currentResource != resource) {
            this.currentResource = resource;
            var rts = this.routesTabsStorage.find(function (rts) { return rts.resource == resource; });
            this.routesTabs = rts ? rts.routesTabs : [];
        }
    };
    UITabRouterView.prototype.getRouteTab = function (navigationInstruction, resource) {
        var routeUniqueTitle = this.extractRouteUniqueTitle(navigationInstruction);
        var rt;
        if (navigationInstruction.fragment == '/')
            rt = this.routesTabs.find(function (r) { return r.fragment == navigationInstruction.fragment && r.resource == resource; });
        else
            rt = this.routesTabs.find(function (r) { return r.name == routeUniqueTitle && r.resource == resource; });
        if (rt == null) {
            if (!navigationInstruction.fragment.includes(common_model_1.Various.NewId.toString())) {
                rt = new UIRouteTab(routeUniqueTitle, navigationInstruction, resource);
                if (!this.routesTabs.some(function (r) { return r.first; })) {
                    rt.first = navigationInstruction.config.nav == true;
                }
                this.routesTabs.push(rt);
            }
        }
        else {
            rt.name = routeUniqueTitle;
        }
        return rt;
    };
    UITabRouterView.prototype.extractResource = function (route) {
        var resource;
        var paramIndex = route.indexOf('/:');
        if (paramIndex > -1) {
            resource = route.slice(0, paramIndex);
        }
        else {
            var allIndex = route.toLowerCase().indexOf('/all');
            if (allIndex > -1) {
                resource = route.slice(0, allIndex);
            }
            else {
                var starIndex = route.toLowerCase().indexOf('/*');
                if (starIndex > -1) {
                    resource = route.slice(0, starIndex);
                }
                else {
                    resource = route;
                }
            }
        }
        return resource;
    };
    UITabRouterView.prototype.extractRoute = function (navigationInstruction) {
        if ((navigationInstruction === null || navigationInstruction === void 0 ? void 0 : navigationInstruction.config) == null)
            return;
        if (navigationInstruction.config.redirect != null)
            return navigationInstruction.config.redirect;
        if (Array.isArray(navigationInstruction.config.route))
            return navigationInstruction.config.route[navigationInstruction.config.route.length - 1];
        if (navigationInstruction.config.route.trim().length > 0)
            return navigationInstruction.config.route;
        return navigationInstruction.config.href;
    };
    UITabRouterView.prototype.extractRouteUniqueTitle = function (navigationInstruction) {
        var _a, _b, _c, _d, _e, _f;
        var routeUniqueTitle;
        var defaultViewport;
        if ((navigationInstruction === null || navigationInstruction === void 0 ? void 0 : navigationInstruction.viewPortInstructions) != null && this.name in (navigationInstruction === null || navigationInstruction === void 0 ? void 0 : navigationInstruction.viewPortInstructions))
            defaultViewport = navigationInstruction === null || navigationInstruction === void 0 ? void 0 : navigationInstruction.viewPortInstructions[this.name];
        if (((_c = (_b = (_a = defaultViewport === null || defaultViewport === void 0 ? void 0 : defaultViewport.controller) === null || _a === void 0 ? void 0 : _a.view) === null || _b === void 0 ? void 0 : _b.bindingContext) === null || _c === void 0 ? void 0 : _c.ribbonHeaderText) != null)
            routeUniqueTitle = defaultViewport.controller.view.bindingContext.ribbonHeaderText;
        else if (((_e = (_d = defaultViewport === null || defaultViewport === void 0 ? void 0 : defaultViewport.component) === null || _d === void 0 ? void 0 : _d.viewModel) === null || _e === void 0 ? void 0 : _e.ribbonHeaderText) != null)
            routeUniqueTitle = defaultViewport.component.viewModel.ribbonHeaderText;
        else if (((_f = navigationInstruction.config) === null || _f === void 0 ? void 0 : _f.name) != null)
            routeUniqueTitle = this.i18n.tr(navigationInstruction.config.name);
        else
            routeUniqueTitle = Array.isArray(navigationInstruction.config.route) ? navigationInstruction.config.route[0] : navigationInstruction.config.route;
        return routeUniqueTitle;
    };
    UITabRouterView.prototype.navigate = function (tab) {
        if (tab.childRoute != null) {
            this.router.navigate(tab.childRoute);
            return;
        }
        if ((tab === null || tab === void 0 ? void 0 : tab.fragment) == null)
            return;
        this.router.navigate(tab.fragment, tab.options);
    };
    UITabRouterView.prototype.removeTab = function (event, tab) {
        var _this = this;
        if ((tab === null || tab === void 0 ? void 0 : tab.fragment) == null)
            return;
        if ((event === null || event === void 0 ? void 0 : event.stopPropagation) != null)
            event.stopPropagation();
        var i = this.routesTabs.findIndex(function (r) { return r.fragment == tab.fragment; });
        if (i < 0)
            return;
        this.routesTabs.splice(i, 1);
        if (tab.active) {
            var firstTab_1 = this.routesTabs[0];
            if (firstTab_1 != null) {
                this.taskQueue.queueTask(function () {
                    _this.navigate(firstTab_1);
                });
            }
        }
    };
    UITabRouterView.prototype.storeRoutesTabs = function (resource, isVisible) {
        if (isVisible === void 0) { isVisible = true; }
        var storage = this.routesTabsStorage.find(function (s) { return s.resource == resource; });
        if (storage == null) {
            storage = new UIRouteTabStorage(resource, isVisible);
            this.routesTabsStorage.push(storage);
        }
        storage.routesTabs = this.routesTabs;
        window.localStorage.setItem('DigiRouteTabs', JSON.stringify(this.routesTabsStorage));
    };
    UITabRouterView.prototype.restoreRoutesTabs = function () {
        var ls = window.localStorage.getItem('DigiRouteTabs');
        if (ls != null) {
            this.routesTabsStorage = JSON.parse(ls);
        }
    };
    UITabRouterView.prototype.clearUnknowRoutes = function (routesTabs) {
        var _this = this;
        routesTabs.forEach(function (routes) {
            routes.routesTabs = routes.routesTabs.filter(function (tab) {
                return _this.router.routes.findIndex(function (r) { var _a; return ((_a = r.navModel) === null || _a === void 0 ? void 0 : _a.relativeHref) == tab.relativeHref; }) > -1
                    && tab.name.toLowerCase() != 'login';
            });
            var firstSingleRoute = routes.routesTabs.filter(function (x) { return x.first; });
            if (firstSingleRoute.length > 1) {
                var routeToRemove = firstSingleRoute.filter(function (x) { return x.fragment != '/'; });
                routeToRemove.forEach(function (x) {
                    routes.routesTabs.remove(x);
                });
            }
        });
        return routesTabs;
    };
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.oneTime }),
        __metadata("design:type", String)
    ], UITabRouterView.prototype, "name", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.oneTime }),
        __metadata("design:type", Boolean)
    ], UITabRouterView.prototype, "tooltip", void 0);
    __decorate([
        (0, aurelia_framework_1.computedFrom)("routesTabs", "routesTabs.length"),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], UITabRouterView.prototype, "routesTabDisplay", null);
    __decorate([
        (0, aurelia_framework_1.computedFrom)("routesTabs", "routesTabs.length"),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], UITabRouterView.prototype, "classes", null);
    UITabRouterView = __decorate([
        (0, aurelia_framework_1.customElement)("ui-tab-router-view"),
        __metadata("design:paramtypes", [aurelia_framework_1.TaskQueue, aurelia_router_1.Router, aurelia_event_aggregator_1.EventAggregator, aurelia_i18n_1.I18N])
    ], UITabRouterView);
    return UITabRouterView;
}());
var UIRouteTab = (function () {
    function UIRouteTab(name, navigationInstruction, resource) {
        var _a, _b;
        this.active = false;
        this.name = name;
        this.fragment = navigationInstruction.fragment;
        this.relativeHref = (_b = (_a = navigationInstruction.config) === null || _a === void 0 ? void 0 : _a.navModel) === null || _b === void 0 ? void 0 : _b.relativeHref;
        this.options = navigationInstruction.options;
        this.resource = resource;
    }
    return UIRouteTab;
}());
var UIRouteTabStorage = (function () {
    function UIRouteTabStorage(resource, isVisible) {
        if (isVisible === void 0) { isVisible = true; }
        this.resource = resource;
        this.isVisible = isVisible;
    }
    return UIRouteTabStorage;
}());

//# sourceMappingURL=ui-tab-router-view.js.map
