"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "RoleService", {
    enumerable: true,
    get: function() {
        return RoleService;
    }
});
const _lodash = require("lodash");
const _authorizationconstants = require("../../constants/authorization.constants");
const _models = require("../../models");
const _runtimeexceptions = require("../../exceptions/runtime.exceptions");
class RoleService {
    settingsStore;
    appDefaultRole;
    appDefaultRoleNoLogin;
    constructor({ appDefaultRole, appDefaultRoleNoLogin, settingsStore }){
        this.settingsStore = settingsStore;
        this.appDefaultRole = appDefaultRole;
        this.appDefaultRoleNoLogin = appDefaultRoleNoLogin;
    }
    _roles = [];
    get roles() {
        return this._roles;
    }
    toDto(role) {
        return {
            id: role.id,
            name: role.name
        };
    }
    async getAppDefaultRole() {
        if (await this.settingsStore.getLoginRequired()) {
            return this.appDefaultRole;
        }
        return this.appDefaultRoleNoLogin;
    }
    getRolesPermissions(roles) {
        let permissions = [];
        if (!roles?.length) return [];
        for (let role of roles){
            const normalizedRole = this.normalizeRoleIdOrName(role);
            const rolePermissions = this.getRolePermissions(normalizedRole);
            permissions = (0, _lodash.union)(permissions, rolePermissions);
        }
        return permissions;
    }
    getRolePermissions(role) {
        const normalizedRole = this.normalizeRoleIdOrName(role);
        return _authorizationconstants.ROLE_PERMS[normalizedRole];
    }
    async getAppDefaultRoleIds() {
        if (!this._roles?.length) {
            await this.syncRoles();
        }
        const guestRole = this.getRoleByName(await this.getAppDefaultRole());
        return [
            guestRole.id
        ];
    }
    async getSynchronizedRoleByName(roleName) {
        if (!this._roles?.length) {
            await this.syncRoles();
        }
        return this.getRoleByName(roleName);
    }
    authorizeRole(requiredRole, assignedRoles) {
        return !!assignedRoles.find((ar)=>{
            const normalizedRole = this.normalizeRoleIdOrName(ar);
            if (!normalizedRole) return false;
            return normalizedRole === requiredRole;
        });
    }
    authorizeRoles(requiredRoles, assignedRoles, subset = true) {
        let isAuthorized = false;
        if (!requiredRoles?.length) return true;
        requiredRoles.forEach((rr)=>{
            const result = this.authorizeRole(rr, assignedRoles);
            isAuthorized = subset ? isAuthorized || result : isAuthorized && result;
        });
        return isAuthorized;
    }
    getRoleByName(roleName) {
        const role = this._roles.find((r)=>r.name === roleName);
        if (!role) throw new _runtimeexceptions.NotFoundException(`Role by provided name not found`);
        return role;
    }
    getManyRoles(roleIds) {
        return roleIds.map((roleId)=>this.getRole(roleId));
    }
    getRole(roleId) {
        const role = this._roles.find((r)=>r.id === roleId);
        if (!role) throw new _runtimeexceptions.NotFoundException(`Role by provided id not found`);
        return role;
    }
    async syncRoles() {
        this._roles = [];
        for (let roleName of Object.values(_authorizationconstants.ROLES)){
            const storedRole = await _models.Role.findOne({
                name: roleName
            });
            if (!storedRole) {
                const newRole = await _models.Role.create({
                    name: roleName
                });
                this._roles.push(newRole);
            } else {
                this._roles.push(storedRole);
            }
        }
    }
    normalizeRoleIdOrName(assignedRole) {
        const roleInstance = this.roles.find((r)=>r.id === assignedRole || r.name === assignedRole);
        if (!roleInstance) {
            console.warn(`The role by provided id did not exist in definition. Skipping`);
            return;
        }
        return roleInstance.name;
    }
}

//# sourceMappingURL=role.service.js.map