"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "RoleService", {
    enumerable: true,
    get: function() {
        return RoleService;
    }
});
const _baseservice = require("./base.service");
const _entities = require("../../entities");
const _roledto = require("../interfaces/role.dto");
const _lodash = require("lodash");
const _runtimeexceptions = require("../../exceptions/runtime.exceptions");
const _authorizationconstants = require("../../constants/authorization.constants");
class RoleService extends (0, _baseservice.BaseService)(_entities.Role, _roledto.RoleDto) {
    appDefaultRole;
    appDefaultRoleNoLogin;
    settingsStore;
    constructor(typeormService, appDefaultRole, appDefaultRoleNoLogin, settingsStore){
        super(typeormService), this.appDefaultRole = appDefaultRole, this.appDefaultRoleNoLogin = appDefaultRoleNoLogin, this.settingsStore = settingsStore;
    }
    _roles = [];
    get roles() {
        return this._roles;
    }
    async getAppDefaultRole() {
        if (await this.settingsStore.getLoginRequired()) {
            return this.appDefaultRole;
        }
        return this.appDefaultRoleNoLogin;
    }
    async getAppDefaultRoleNames() {
        if (!this._roles?.length) {
            await this.syncRoles();
        }
        return [
            await this.getAppDefaultRole()
        ];
    }
    toDto(role) {
        return {
            id: role.id,
            name: role.name
        };
    }
    getRolesPermissions(roleNames) {
        let permissions = [];
        if (!roleNames?.length) return [];
        for (const roleName of roleNames){
            const rolePermissions = this.getRolePermissions(roleName);
            permissions = (0, _lodash.union)(permissions, rolePermissions);
        }
        return permissions;
    }
    authorizeRoles(requiredRoles, assignedRoles, subset) {
        if (!requiredRoles?.length) return true;
        let isAuthorized = !subset;
        for (const requiredRole of requiredRoles){
            const result = this.authorizeRole(requiredRole, assignedRoles);
            isAuthorized = subset ? isAuthorized || result : isAuthorized && result;
        }
        return isAuthorized;
    }
    authorizeRole(requiredRole, assignedRoles) {
        return assignedRoles.includes(requiredRole);
    }
    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 was not found`);
        return role;
    }
    getRoleByName(roleName) {
        const role = this._roles.find((r)=>r.name === roleName);
        if (!role) throw new _runtimeexceptions.NotFoundException(`Role by provided name was not found`);
        return role;
    }
    getRolePermissions(roleName) {
        if (!roleName) {
            return [];
        }
        return _authorizationconstants.ROLE_PERMS[roleName] ?? [];
    }
    roleIdsToRoleNames(roleIds) {
        return roleIds.map((roleId)=>{
            const role = this._roles.find((r)=>r.id === roleId);
            return role?.name;
        }).filter((name)=>name !== undefined);
    }
    async getSynchronizedRoleByName(roleName) {
        if (!this._roles?.length) {
            await this.syncRoles();
        }
        return this.getRoleByName(roleName);
    }
    async syncRoles() {
        this._roles = [];
        for (const roleName of Object.values(_authorizationconstants.ROLES)){
            const storedRole = await this.repository.findOneBy({
                name: roleName
            });
            if (storedRole) {
                this._roles.push(storedRole);
            } else {
                const newRole = await this.create({
                    name: roleName
                });
                this._roles.push(newRole);
            }
        }
    }
}

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