import { expect } from '@core/fixtures';
import { Page } from 'playwright';
import { AutoBind, describedStep } from '../utils';

type HasAlertOptions = {
  content?: string;
  header?: string;
  variant?: string;
};

export class GlobalAlert extends AutoBind {
  constructor(public page: Page) {
    super();
  }

  private alertBaseLocator() {
    return this.page.locator('[data-role="global-alert"] [role="alert"]');
  }

  @describedStep('PageHasAlert', { withArgs: true })
  async hasAlert({ content, header, variant }: HasAlertOptions = {}) {
    let l = this.alertBaseLocator();
    if (header) {
      l = l.filter({
        has: this.page.locator('.alert-heading', { hasText: header }),
      });
    }

    if (content) {
      l = l.filter({ hasText: content });
    }
    if (variant) {
      l = l.filter({
        has: this.page.locator(`:scope.alert-${variant}`),
      });
    }

    await expect(l.first()).toBeVisible();
  }
}
