import CookieStorage from '../configs/CookieStorage';
import { UserStatus } from '../configs/sessionTypes';
const hasAccess = (uid, roles, mode) => {
    const token = CookieStorage.getJwtToken();
    if (!token)
        return false;
    const logged = parseJwt(token);
    if (logged.status && logged.status === UserStatus.SUPER)
        return true;
    return roles.some((role) => logged.permissions.includes(`ROLE_${role}_${mode.toUpperCase()}_${uid}`));
};
const parseJwt = (token) => {
    const base64Url = token.split('.')[1];
    const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
    const payload = decodeURIComponent(window
        .atob(base64)
        .split('')
        .map(function (c) {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    })
        .join(''));
    return JSON.parse(payload);
};
const SecurityUtils = {
    hasAccess,
    parseJwt,
};
export default SecurityUtils;
