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((t) => t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).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*)$/);
}
