import NProgress from "nprogress";
import { useAbilityStore } from "@/stores/ability";
import { useNavigationStore } from "@/stores/admin/navigation";

export async function abilityAndNavUpdate(to: any, from: any, next: any) {
  NProgress.start();
  const ability = useAbilityStore();
  const navigation = useNavigationStore();

  let type = to.meta.type;
  let section = to.meta.section;
  let module = to.meta.module;

  navigation.activeNavigation = to.name.split("-")[1];
  navigation.activeLink = to.name.split("-")[2];

  if (ability.can(type, section, module)) {
    NProgress.done();
    next();
  } else {
    NProgress.done();
    next({ name: "admin-default" });
  }
}

export async function isOwner(to: any, from: any, next: any) {
  NProgress.start();
  const ability = useAbilityStore();

  if (ability.isOwner) {
    NProgress.done();
    next();
  } else {
    NProgress.done();
    next(false);
  }
}
