import { trackEvent } from "./base"

import type {
  PaymentEvent,
  PaymentFailEvent,
} from "@/types/components/tracking"

function isPaymentFailEvent(event: PaymentEvent): event is PaymentFailEvent {
  return "errorMessage" in event
}

export function trackUpdatePaymentMethod(hotelId: string, method: string) {
  trackEvent({
    event: "paymentSelection",
    hotelInfo: {
      hotelId: hotelId,
    },
    cta: {
      name: method,
    },
  })
}

export function trackPaymentEvent(paymentEvent: PaymentEvent) {
  const paymentAttempt = {
    event: paymentEvent.event,
    hotelInfo: {
      hotelId: paymentEvent.hotelId,
    },
    paymentInfo: {
      isSavedCard: paymentEvent.isSavedCreditCard,
      status: paymentEvent.status,
      type: paymentEvent.method,
      smsEnable: paymentEvent.smsEnable,
      errorMessage: isPaymentFailEvent(paymentEvent)
        ? paymentEvent.errorMessage
        : undefined,
    },
  }
  trackEvent(paymentAttempt)
}
