import type { Page } from '@playwright/test'

import type { AdminUrlUtil } from '../shared/adminUrlUtil.js'

import { waitForFormReady } from './helpers.js'

export const goToFirstCell = async (page: Page, serverURL: string) => {
  const cellLink = page.locator(`tbody tr:first-child td a`).first()
  const linkURL = await cellLink.getAttribute('href')
  await page.goto(`${serverURL}${linkURL}`)
}

export const navigateToDoc = async (page: Page, urlUtil: AdminUrlUtil) => {
  await page.goto(urlUtil.list)
  // wait for query params to arrive
  const regex = new RegExp(`^${urlUtil.list}(?:\\?.*)?$`)
  await page.waitForURL(regex)
  await goToFirstCell(page, urlUtil.serverURL)
  await waitForFormReady(page)
}

export const navigateToTrashedDoc = async (page: Page, urlUtil: AdminUrlUtil) => {
  await page.goto(urlUtil.trash)
  // wait for query params to arrive
  const regex = new RegExp(`^${urlUtil.trash}(?:\\?.*)?$`)
  await page.waitForURL(regex)
  await goToFirstCell(page, urlUtil.serverURL)
}
