import { useUserStore } from '@/stores/modules/user';
import { MenuType } from '@/enum/app';

export const checkPermission = (mark: any) => {
  // mark 是这样的形式 module + '@' + 'controller@action'
  // 当然也可以定义其他形式的
  console.log('permission.type', mark);
  const userStore = useUserStore();

  const permissions = userStore.getPermissions;
  const has = permissions?.some(permission => {
    const a: string =
      permission.module + '.' + permission.permission_mark.replace('@', '.');
    return mark.toLowerCase() === a.toLowerCase();
  });
  console.log('has', has);
  return has;
};

export const getFirstPath = (pid = 0) => {
  let routeStr = getFirstPathString(pid);

  // console.log('routeStr',routeStr);
  return routeStr;
};

function getFirstPathString(pid = 0, routeStr = '') {
  const userStore = useUserStore();

  const permissions = userStore.getPermissions;
  let flag = false;
  permissions.forEach(permission => {
    // console.log('pid',pid,'routeStr',routeStr,'flag',flag,'permission',permission);
    if (parseInt(permission.parent_id) == parseInt(pid)) {
      flag = true;
      pid = permission.id;
      routeStr =
        routeStr > '' ? routeStr + '/' + permission.route : permission.route;
      return routeStr;
    }
  });
  if (flag) {
    return getFirstPathString(pid, routeStr);
  }
  return routeStr.replace(/\/$/, '');
}
