import { ExceptionCode } from "../exception-code";
import type { ExceptionResult } from "../exception-result";

export class PaymentExceptionHandler {
  static canHandle(error: unknown): error is Error {
    if (!(error instanceof Error)) return false;
    const message = error.message.toLowerCase();
    return (
      message.includes("payment") ||
      message.includes("stripe") ||
      message.includes("ezpay") ||
      message.includes("order")
    );
  }

  static handle(error: Error): ExceptionResult {
    const message = error.message.toLowerCase();
    if (message.includes("cancel")) {
      return {
        code: ExceptionCode.cancelled,
        message: "Payment was cancelled.",
      };
    }

    return {
      code: ExceptionCode.payment,
      message: "Payment could not be completed. Please try again.",
    };
  }
}
