import { expect, type Page, test } from '@playwright/test';
import { createBrowserWorkspace } from './common';

const saveShortcut = process.platform === 'darwin' ? 'Meta+s' : 'Control+s';

async function selectFastPreview(page: Page) {
  await page.getByRole('button', { name: 'Compiler settings' }).click();
  const mode = page.getByLabel('pdfLaTeX mode');
  await mode.click();
  await page.getByRole('option', { name: 'Fast Preview' }).click();
  await page.getByRole('button', { name: 'Close', exact: true }).click();
}

test('R14.24.1 records real compile performance and retains PDF reading state through recompile', async ({
  page,
}) => {
  test.setTimeout(360_000);
  const workspaceName = `cert-r14-24-1-${Date.now()}`;
  await createBrowserWorkspace(page, { workspaceName });

  const source = page.getByLabel('LaTeX source editor');
  await source.fill([
    '\\documentclass{article}',
    '\\begin{document}',
    'R14.24.1 page one.',
    '\\newpage',
    'R14.24.1 page two.',
    '\\newpage',
    'R14.24.1 page three.',
    '\\end{document}',
    '',
  ].join('\n'));
  await source.press(saveShortcut);
  await expect(page.getByText('Saved', { exact: true })).toBeVisible();

  await selectFastPreview(page);
  await page.getByRole('button', { name: 'Compile', exact: true }).click();

  const preview = page.getByTestId('interactive-pdf-preview');
  await expect(preview.locator('canvas').first()).toBeVisible({ timeout: 180_000 });
  await expect(preview).toHaveAttribute('data-output-tab', 'pdf');

  await preview.getByRole('button', { name: 'Next PDF page' }).click();
  await expect(preview).toHaveAttribute('data-page-number', '2');
  await preview.getByRole('button', { name: 'Zoom in' }).click();
  await expect(preview).toHaveAttribute('data-fit-width', 'false');
  await expect(preview).toHaveAttribute('data-zoom', '1.10');

  await preview.getByTestId('interactive-output-tabs').getByRole('button', { name: /Log/ }).click();
  await expect(preview).toHaveAttribute('data-output-tab', 'log');
  const performance = preview.getByTestId('interactive-compile-performance');
  await expect(performance).toBeVisible();
  await expect(performance).toContainText(/Compile performance/);

  // A real recompile must preserve the user's reading/output state rather than
  // forcing page 1/PDF tab or resetting zoom/fit mode.
  await page.getByRole('button', { name: 'Compile', exact: true }).click();
  await expect(preview.getByTestId('interactive-compile-log')).toBeVisible({ timeout: 180_000 });
  await expect(preview).toHaveAttribute('data-output-tab', 'log');
  await expect(preview).toHaveAttribute('data-page-number', '2');
  await expect(preview).toHaveAttribute('data-fit-width', 'false');
  await expect(preview).toHaveAttribute('data-zoom', '1.10');

  await preview.getByTestId('interactive-output-tabs').getByRole('button', { name: 'PDF' }).click();
  await expect(preview.locator('canvas').first()).toBeVisible();
  await expect(preview).toHaveAttribute('data-page-number', '2');
});
