"use strict";
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.applyDefaultPermissionsConfig = exports.addUsersToRoutes = exports.addRolesToRoutes = void 0;
var aurelia_framework_1 = require("aurelia-framework");
var constants_1 = require("./../../constants/constants");
var common_model_1 = require("./../../model/common-model");
;
;
function findInsertionIndex(routes, search) {
    return routes.findIndex(function (x) { return x.route == search || x.moduleId == search || x.name == search || x.href == search; });
}
function insertRoutes(routes, newRoutes, config) {
    var index = -1;
    if (config.after) {
        index = findInsertionIndex(routes, config.after);
        if (index >= 0) {
            index++;
        }
    }
    if (config.before && index < 0) {
        index = findInsertionIndex(routes, config.before);
    }
    routes.splice.apply(routes, __spreadArray([index, 0], __read(newRoutes), false));
    return routes;
}
function addRolesToRoutes(routes, inputConfig, srcPrefix) {
    var _a;
    if (inputConfig === void 0) { inputConfig = {}; }
    if (srcPrefix === void 0) { srcPrefix = 'digiwall-lib'; }
    var config = { group: constants_1.SecurityRouteGroup, routePrefix: 'roles', after: 'users', userRouteName: 'users', detailTitle: 'common:role.role', listTitle: 'common:role.roles', detailModuleId: aurelia_framework_1.PLATFORM.moduleName(srcPrefix + '/vm/security/roles/role-detail'), listModuleId: aurelia_framework_1.PLATFORM.moduleName(srcPrefix + '/vm/security/roles/role-list') };
    Object.assign(config, inputConfig);
    var userRoute = routes.find(function (x) { return x.name == config.userRouteName; });
    var newRoutes = [
        {
            route: "".concat(config.routePrefix, "/:param1"), name: 'roles-detail', moduleId: config.detailModuleId,
            title: config.detailTitle, nav: false, settings: { resource: constants_1.EntityTypeNames.Role, action: constants_1.Actions.Read, params: { userResource: (_a = userRoute === null || userRoute === void 0 ? void 0 : userRoute.settings) === null || _a === void 0 ? void 0 : _a.resource } }, href: "".concat(config.routePrefix, "/").concat(common_model_1.Various.NewId)
        },
        {
            route: "".concat(config.routePrefix, "/all"), name: 'roles', moduleId: config.listModuleId,
            title: config.listTitle, nav: true, settings: { group: config.group, resource: constants_1.EntityTypeNames.Role, action: constants_1.Actions.Read }, href: "".concat(config.routePrefix, "/all")
        },
    ];
    return insertRoutes(routes, newRoutes, config);
}
exports.addRolesToRoutes = addRolesToRoutes;
;
function addUsersToRoutes(routes, config, rolesMode, srcPrefix, addDetailRoute) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j;
    var _k, _l, _m, _o;
    if (config === void 0) { config = {}; }
    if (rolesMode === void 0) { rolesMode = 'none'; }
    if (srcPrefix === void 0) { srcPrefix = 'digiwall-lib'; }
    if (addDetailRoute === void 0) { addDetailRoute = true; }
    var commonSettings = { group: constants_1.SecurityRouteGroup, resource: constants_1.EntityTypeNames.User };
    (_a = config.routePrefix) !== null && _a !== void 0 ? _a : (config.routePrefix = 'users');
    (_b = config.list) !== null && _b !== void 0 ? _b : (config.list = {});
    (_c = (_k = config.list).moduleId) !== null && _c !== void 0 ? _c : (_k.moduleId = aurelia_framework_1.PLATFORM.moduleName(srcPrefix + '/vm/security/users/user-list'));
    (_d = (_l = config.list).title) !== null && _d !== void 0 ? _d : (_l.title = 'common:user.users');
    (_e = config.detail) !== null && _e !== void 0 ? _e : (config.detail = {});
    (_f = (_m = config.detail).moduleId) !== null && _f !== void 0 ? _f : (_m.moduleId = aurelia_framework_1.PLATFORM.moduleName(srcPrefix + "/vm/security/users/user-detail-".concat(rolesMode)));
    (_g = (_o = config.detail).title) !== null && _g !== void 0 ? _g : (_o.title = 'common:user.user');
    var newRoutes = [
        {
            route: "".concat(config.routePrefix, "/all"), name: 'users', moduleId: config.list.moduleId,
            title: config.list.title, nav: true, settings: Object.assign({}, commonSettings, config, ((_h = config.list) === null || _h === void 0 ? void 0 : _h.settings) || { action: constants_1.Actions.Read }),
            href: "".concat(config.routePrefix, "/all")
        }
    ];
    if (addDetailRoute) {
        newRoutes.push({
            route: "".concat(config.routePrefix, "/:param1"), name: 'users-detail', moduleId: config.detail.moduleId,
            title: config.detail.title, nav: false, settings: Object.assign({}, commonSettings, config, ((_j = config.detail) === null || _j === void 0 ? void 0 : _j.settings) || { action: constants_1.Actions.Read }),
            href: "".concat(config.routePrefix, "/").concat(common_model_1.Various.NewId)
        });
    }
    return insertRoutes(routes, newRoutes, config);
}
exports.addUsersToRoutes = addUsersToRoutes;
;
function applyDefaultPermissionsConfig(config) {
    var _a, _b, _c;
    var _d, _e;
    config !== null && config !== void 0 ? config : (config = {});
    (_d = config.perContext) !== null && _d !== void 0 ? _d : (config.perContext = []);
    (_a = config.perContext).push.apply(_a, [
        { context: 'agGridViewPublic', qualifiedName: ['common:aggridview.system'], isCrud: false },
        { context: 'agGridView', qualifiedName: ['common:aggridview.system'], isCrud: false }
    ]);
    config.permissionNames = Object.assign({
        'ag-grid-view-public': (_b = {},
            _b[constants_1.Actions.Create] = 'common:aggridview.agGridViewPublic',
            _b),
        'ag-grid-view': (_c = {},
            _c[constants_1.Actions.Read] = 'common:aggridview.agGridView',
            _c)
    }, (_e = config.permissionNames) !== null && _e !== void 0 ? _e : {});
    return config;
}
exports.applyDefaultPermissionsConfig = applyDefaultPermissionsConfig;
;

//# sourceMappingURL=security-helpers.js.map
