import { BookingStripePaymentElement } from '@/apps/portal/modules/booking/components/BookingStripePaymentElement';
import { BookingSuccessView } from '@/apps/portal/modules/booking/views/BookingSuccessView';
import { BaseView } from '@/shared/modules/base/views/BaseView';
import { IPaymentMethod } from '@/shared/modules/payment-method/types/payment-method-types';

export class BookingPaymentView extends BaseView {
  readonly stripeBody = this.host.getByTitle('Secure payment input frame').frameLocator(':scope').locator('body');
  readonly stripePaymentElement = new BookingStripePaymentElement(this.stripeBody);
  readonly stripeFieldError = this.stripeBody.locator('.Error');
  readonly stripeAlertError = this.host.getByTestId('stripe-error');
  readonly payNowButton = this.host.getByTestId('pay-now-button');

  async fill(paymentMethod: IPaymentMethod): Promise<void> {
    switch (paymentMethod.kind) {
      case 'card':
        await this.stripePaymentElement.fillCardInfo(paymentMethod);
        break;
      case 'sepa':
        await this.stripePaymentElement.fillSepaInfo(paymentMethod);
        break;
    }
  }

  async confirm(): Promise<BookingSuccessView> {
    await Promise.all([this.host.waitForURL('**/checkout/success?*lang=en'), this.payNowButton.click()]);
    return new BookingSuccessView(this.host);
  }
}
