import store from '@/store';

export function hasPerimission(value) {
  const all_permission = '*:*:*';
  const permissions = (store.getters && store.getters.userPermissions) || [];
  const currentValue = typeof value === 'string' ? [value] : value;
  if (currentValue && currentValue instanceof Array) {
    const permissionFlag = currentValue;
    const hasPermissions = permissions.some((permission) => {
      return (
        permission === all_permission || permissionFlag.includes(permission)
      );
    });
    if (hasPermissions) {
      return true;
    }
  } else {
    throw new Error(`请设置操作权限标签值`);
  }
  return false;
}

export function hasRole(value) {
  const permissions = store.getters && store.getters.userRoles;
  const currentValue = typeof value === 'string' ? [value] : value;

  if (
    currentValue &&
    currentValue instanceof Array &&
    currentValue.length > 0
  ) {
    const permissionFlag = currentValue;
    const hasPermissions = permissions.some((permission) => {
      return permissionFlag.includes(permission);
    });
    if (hasPermissions) {
      return true;
    }
  } else {
    throw new Error(`请设置角色`);
  }
  return false;
}
