import { BaseDialog } from '@/manager/shared/base/BaseDialog';
import { AddCardPaymentMethodForm } from '@/manager/modules/ui/payment-method/components/AddCardPaymentMethodForm';
import { AddSepaPaymentMethodForm } from '@/manager/modules/ui/payment-method/components/AddSepaPaymentMethodForm';
import { getSnackbarHost } from '@/manager/shared/utils/host-utils';
import { IPaymentMethod } from '@/shared/modules/payment-method/types/payment-method-types';
import { Page } from '@playwright/test';

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

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

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

  async fill(paymentMethod: IPaymentMethod): 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()]);
  }
}
