import { BaseComponent } from '@/shared/base/BaseComponent';
import { Locator } from '@playwright/test';

export class BookingInsuranceList extends BaseComponent {
  private readonly loadMoreButton = this.host.locator('[data-test-id=load-more-button]:not(:disabled)');

  async select(insurance: string): Promise<void> {
    const card = this.getInsuranceCard(insurance);
    let cardClicked = false;

    do {
      try {
        await card.click({ timeout: 2500 });
        cardClicked = true;
      } catch (e) {
        await this.loadMoreButton.click();
      }
    } while (!cardClicked);
  }

  private getInsuranceCard(item: string): Locator {
    return this.host.locator(
      `[data-test-id=insurance-card]:has([data-test-id=insurance-card-title]:text-is("${item}"))`
    );
  }
}
