import type { Permission } from './permissions';

export function hasPermission(
  permissions: readonly string[] | null | undefined,
  permission: Permission
) {
  return permissions?.includes(permission) ?? false;
}

export function hasAnyPermission(
  permissions: readonly string[] | null | undefined,
  required: readonly Permission[]
) {
  if (!required.length) {
    return true;
  }

  return required.some((permission) => hasPermission(permissions, permission));
}

export function hasAllPermissions(
  permissions: readonly string[] | null | undefined,
  required: readonly Permission[]
) {
  if (!required.length) {
    return true;
  }

  return required.every((permission) => hasPermission(permissions, permission));
}
