import { AbilityBuilder, createMongoAbility } from "@casl/ability";

export function defineAbilitiesFor(user) {
  const { can, cannot, build } = new AbilityBuilder(createMongoAbility);

  if (user) {
    can("access", "Dashboard");
    cannot("access", "Login");
    const parsedData = JSON.parse(user.value);

    if (parsedData.roles.find((item) => item === "representation")) {
      can("access", "Panel");
    }
  } else {
    can("access", "Login");
    cannot("access", "Dashboard");
  }

  return build();
}
