import { escapeRegExp } from '@/shared/utils/regex-utils';
import { expect, Locator } from '@playwright/test';

export async function expectMultiLocatorToHaveText(locator: Locator, text: string[] | undefined): Promise<void> {
  if (!text) {
    await expectLocatorToBeEmpty(locator);
    return;
  }

  const expectedValue = RegExp(text.map(escapeRegExp).join('|'));

  for (const element of await locator.all()) {
    await expectSingleLocatorToHaveText(element, expectedValue);
  }
}

export async function expectSingleLocatorToHaveText(
  locator: Locator,
  text: string | RegExp | undefined
): Promise<void> {
  text
    ? await expect(locator).toHaveText(text, { ignoreCase: true, useInnerText: true })
    : await expectLocatorToBeEmpty(locator);
}

export async function expectLocatorToBeEmpty(locator: Locator): Promise<void> {
  await expect(locator).toHaveText(/^(\s*-?\s*)$/);
}
