import { BasePage } from '@/manager/shared/base/BasePage';
import { IDiscountListFilter } from '@/manager/modules/discount/types/discount-types';
import { DiscountCreateDialog } from '@/manager/modules/discount/views/DiscountCreateDialog';
import { DiscountDetailPage } from '@/manager/modules/discount/views/DiscountDetailPage';
import { DataTable } from '@/manager/modules/ui/data-table/components/DataTable';
import { DebouncedTextField } from '@/manager/shared/components/DebouncedTextField';
import { MultiAutocomplete } from '@/manager/shared/components/MultiAutocomplete';
import { getFilterHost, getPageHeaderActionButtonHost } from '@/manager/shared/utils/host-utils';

export class DiscountListPage extends BasePage {
  readonly actionButton = getPageHeaderActionButtonHost(this.header);
  readonly searchTextField = new DebouncedTextField(getFilterHost(this.body, 'Search'));
  readonly locationAutocomplete = new MultiAutocomplete(getFilterHost(this.body, 'Location'));
  readonly dataTable = new DataTable(this.body);

  async filter(filter: IDiscountListFilter): Promise<void> {
    filter.search && (await this.searchTextField.fill(filter.search.value));
    filter.locations && (await this.locationAutocomplete.fill(filter.locations.value));
  }

  async openDiscountCreateDialog(): Promise<DiscountCreateDialog> {
    await this.dataTable.waitForLoading();
    await Promise.all([this.host.waitForURL('/discounts/create'), this.actionButton.click()]);
    return new DiscountCreateDialog(this.host);
  }

  async goToDiscountDetailPage(index: number): Promise<DiscountDetailPage> {
    await Promise.all([this.host.waitForURL('**/discounts/*'), this.dataTable.clickRow(index)]);
    return new DiscountDetailPage(this.host);
  }
}
