import { BaseDialog } from '@/manager/shared/base/BaseDialog';
import { CardPaymentMethodAddForm } from '@/manager/modules/ui/payment-method/components/CardPaymentMethodAddForm';
import { SepaPaymentMethodAddForm } from '@/manager/modules/ui/payment-method/components/SepaPaymentMethodAddForm';
import { getSnackbarHost } from '@/manager/shared/utils/locator-utils';
import { PaymentMethod } from '@/shared/modules/payment-method/payment-method-types';
import { Page } from '@playwright/test';

export class StripePaymentElementAddDialog extends BaseDialog {
  readonly sepaForm = new SepaPaymentMethodAddForm(this.main);
  readonly cardForm = new CardPaymentMethodAddForm(this.main);
  readonly snackbar = getSnackbarHost(this.host, 'Payment method added');

  constructor(host: Page) {
    super(host, 'add-stripe-payment-element-dialog');
  }

  async add(paymentMethod: PaymentMethod): Promise<void> {
    await this.fill(paymentMethod);
    await this.submit();
  }

  async fill(paymentMethod: PaymentMethod): Promise<void> {
    switch (paymentMethod.kind) {
      case 'card':
        await this.cardForm.fill(paymentMethod);
        break;
      case 'sepa':
        await this.sepaForm.fill(paymentMethod);
        break;
    }
  }

  async submit(): Promise<void> {
    await Promise.all([this.snackbar.waitFor(), this.submitButton.click()]);
  }
}
