import AppError from '@/shared/errors/app-error';
import {
  getNotification,
  isNotification,
} from '@/shared/notifications/helpers';

const DEFAULT_CODE = 'errors.unauthorized.message';

class UnauthorizedError extends AppError {
  constructor(messageCode?: string) {
    super(401, UnauthorizedError.resolveMessage(messageCode), {
      code: messageCode ?? DEFAULT_CODE,
    });
  }

  private static resolveMessage(messageCode?: string): string {
    if (messageCode && isNotification(messageCode)) {
      return getNotification(messageCode);
    }
    return getNotification(DEFAULT_CODE);
  }
}

export default UnauthorizedError;
