import { DataTable } from '@/manager/modules/ui/data-table/components/DataTable';
import { expectMultiLocatorToHaveText, expectSingleLocatorToHaveText } from '@/manager/shared/utils/expect-utils';
import { getChipContentHost } from '@/manager/shared/utils/locator-utils';
import { escapeRegExp } from '@/shared/utils/regex-utils';

export async function expectDataTableChipsColumnToHaveText(
  table: DataTable,
  columnId: string,
  text: string | string[] | undefined
): Promise<void> {
  const column = table.getColumn(columnId);
  const expectedText = typeof text === 'string' ? [text] : text;

  for (const cell of await column.all()) {
    await expectMultiLocatorToHaveText(getChipContentHost(cell), expectedText);
  }
}

export async function expectDataTableTextColumnToHaveText(
  table: DataTable,
  columnId: string,
  text: string | undefined
): Promise<void> {
  const column = table.getColumn(columnId);

  for (const cell of await column.all()) {
    await expectSingleLocatorToHaveText(cell, text);
  }
}

export async function expectDataTableColumnToContainText(
  table: DataTable,
  columnId: string,
  text: string | string[] | undefined
): Promise<void> {
  const column = table.getColumn(columnId);
  const expectedValue =
    text == null ? undefined : RegExp(typeof text === 'string' ? escapeRegExp(text) : text.map(escapeRegExp).join('|'));

  for (const cell of await column.all()) {
    await expectSingleLocatorToHaveText(cell, expectedValue);
  }
}
