import { HttpStatusCode, HttpStatusCodeHelper } from "./HttpStatusCode";
import { GeneralError } from "./GeneralError";

const getReadableLog = (message: string, statusCode: number): string => {
  const httpStatusCode = HttpStatusCodeHelper.fromStatusCode(statusCode);

  return `Error data: statusCode: ${
    httpStatusCode || "Unknown Code"
  }, message: ${message || "No error message available"}, readableStatus: ${
    HttpStatusCode[httpStatusCode] || "Unknown Code."
  }`;
};

export class NetworkError extends GeneralError {
  readonly originalMessage: string;
  static isNetworkError(error: Error): boolean {
    return error instanceof NetworkError;
  }

  get httpStatusCode(): HttpStatusCode {
    return HttpStatusCodeHelper.fromStatusCode(this.statusCode);
  }

  constructor(message: string, statusCode: number) {
    super(getReadableLog(message, statusCode), statusCode);

    this.originalMessage = message;
  }

  public isTokenExpired = (): boolean =>
    this.httpStatusCode === HttpStatusCode.Unauthorized;

  public isNoAccess = (): boolean =>
    this.httpStatusCode === HttpStatusCode.Forbidden;

  public isPaymentRequired = (): boolean =>
    this.httpStatusCode === HttpStatusCode.PaymentRequired;
}
