import type { Page } from '@playwright/test'
import { orderedPages, getPagePath } from '../../playground/docs/navigation'

const SCREENSHOT_OPTS = {
  maxDiffPixelRatio: 0.001,
  maxDiffPixels: 100,
  animations: 'disabled',
} as const

if (process.env.VITEST) {
  const { test } = await import('vitest')
  test.skip('Playwright visual suite is run via pnpm test:visual', () => {})
} else {
  const { test, expect } = await import('@playwright/test')

  for (const pageItem of orderedPages) {
    test(`${pageItem.id}`, async ({ page }: { page: Page }) => {
      await page.goto(getPagePath(pageItem.id))
      await page.waitForLoadState('networkidle')

      await expect(page).toHaveScreenshot(`${pageItem.id}-dark.png`, SCREENSHOT_OPTS)

      await page.click('button.canonical-toggle')
      await page.waitForFunction(
        () => document.documentElement.getAttribute('data-theme') === 'light',
      )

      await expect(page).toHaveScreenshot(`${pageItem.id}-light.png`, SCREENSHOT_OPTS)
    })
  }
}
