/**
 * Function สำหรับเช็ค permission ของ user
 */
import { useMenuDataStore } from "@/stores/menuList";
import type { RoleData } from "@/interface/main";

const storeMenu = useMenuDataStore();

export function checkPermission(route: any): RoleData | null {
  if (storeMenu.permissions && route.meta.Key) {
    const role =
      storeMenu.permissions.roles.find(
        (item) => item.authSysId === route.meta.Key
      ) ?? null;

    return role;
  } else {
    return null;
  }
}

export function checkPermissionCreate(sys: string) {
  if (storeMenu.permissions && sys) {
    return (
      storeMenu.permissions.roles.find(
        (item) => item.authSysId === sys && item.attrIsCreate
      ) ?? false
    );
  } else {
    return false;
  }
}

export function checkPermissionList(sys: string[]) {
  if (storeMenu.permissions && sys) {
    return (
      storeMenu.permissions.roles.some(
        (item) => sys.includes(item.authSysId) && item.attrIsList
      ) ?? false
    );
  } else {
    return false;
  }
}

export function checkPermissionGet(sys: string) {
  if (storeMenu.permissions && sys) {
    return (
      storeMenu.permissions.roles.some(
        (item) => sys.includes(item.authSysId) && item.attrIsGet
      ) ?? false
    );
  } else {
    return false;
  }
}
