import {Ability} from "@casl/ability";

const {AbilityBuilder} = require("@casl/ability");

export function createUserAbility(abilities) {
    const { can, rules } = new AbilityBuilder();

    abilities.forEach((ability) => {
        const stringList = ability.split('_');
        const action = stringList[0];
        let type = ""
        for(let i=1; i<stringList.length; i++){
            if(i !== 1 && i <= stringList.length){
                type = type + "_";
            }
            type = type + stringList[i];
        }
        can(action,type)
    });

    return new Ability(rules);
}