import { BaseDialog } from '@/manager/shared/base/BaseDialog';
import { InsuranceEditBookingPortalData } from '@/manager/modules/insurance/insurance-types';
import { InsuranceDetailsPage } from '@/manager/modules/insurance/views/InsuranceDetailsPage';
import { LanguageAwareInput } from '@/manager/shared/components/LanguageAwareInput';
import { getSnackbarHost } from '@/manager/shared/utils/locator-utils';
import { Page } from '@playwright/test';

export class InsuranceEditBookingPortalDialog extends BaseDialog {
  readonly shortDescriptionInput = new LanguageAwareInput(this.main);
  readonly snackbar = getSnackbarHost(this.host, 'Action successfully executed!');

  constructor(host: Page) {
    super(host, 'insurance-edit-booking-portal-dialog');
  }

  async edit(data: InsuranceEditBookingPortalData): Promise<InsuranceDetailsPage> {
    await this.fill(data);
    await Promise.all([this.snackbar.waitFor(), this.host.waitForURL('**/insurances/*'), this.submit()]);
    return new InsuranceDetailsPage(this.host);
  }

  async fill(data: InsuranceEditBookingPortalData): Promise<void> {
    if (data.shortDescription != null) {
      await this.shortDescriptionInput.fill(data.shortDescription);
    }
  }

  async submit(): Promise<void> {
    await this.submitButton.click();
  }
}
