import { BaseDialog } from '@/manager/shared/base/BaseDialog';
import { PriceChangeRadioGroup } from '@/manager/modules/ui/price-change/components/PriceChangeRadioGroup';
import { PriceChange } from '@/manager/modules/ui/price-change/price-change-types';
import { PriceChangeConfirmDialog } from '@/manager/modules/ui/price-change/views/PriceChangeConfirmDialog';
import { getDialogErrorsHost, getSnackbarHost } from '@/manager/shared/utils/locator-utils';
import { Page } from '@playwright/test';

export class PriceChangeDialog extends BaseDialog {
  readonly priceChangeRadioGroup = new PriceChangeRadioGroup(this.main);
  readonly errors = getDialogErrorsHost(this.main);
  readonly snackbar = getSnackbarHost(this.host, 'Prices changed successfully');

  constructor(host: Page) {
    super(host, 'edit-price-dialog');
  }

  async edit(data: PriceChange): Promise<void> {
    await this.fill(data);
    await this.submit();
    return this.confirm();
  }

  async fill(data: PriceChange): Promise<void> {
    await this.priceChangeRadioGroup.fill(data);
  }

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

  async confirm(): Promise<void> {
    const confirmDialog = new PriceChangeConfirmDialog(this.host);
    await Promise.all([this.snackbar.waitFor(), confirmDialog.submit()]);
  }
}
