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

test('R14.19 source preferences reconfigure the same CodeMirror view and persist', async ({ page }) => {
  const workspaceName = `source-r14-19-${Date.now()}`;
  await createBrowserWorkspace(page, { workspaceName });
  const host = page.locator('[data-latex-codemirror]');
  const editor = host.locator('.cm-editor');
  const settings = page.getByTestId('latex-source-editor-settings');
  await expect(editor).toBeVisible();
  await editor.evaluate((element) => { (element as HTMLElement).dataset.r1419Identity = 'same'; });

  await settings.locator('summary').click();
  await page.getByTestId('latex-font-size').selectOption('20');
  await page.getByTestId('latex-line-numbers').uncheck();
  await page.getByTestId('latex-spellcheck').uncheck();
  await page.getByTestId('latex-toolbar-visible').uncheck();

  await expect(editor).toHaveAttribute('data-r1419-identity', 'same');
  await expect(host.locator('.cm-lineNumbers')).toHaveCount(0);
  await expect(page.getByLabel('LaTeX source editor')).toHaveAttribute('spellcheck', 'false');
  await expect(page.getByTestId('latex-plugin-toolbar')).toHaveCount(0);

  await page.reload({ waitUntil: 'domcontentloaded' });
  await expect(host.locator('.cm-lineNumbers')).toHaveCount(0);
  await expect(page.getByLabel('LaTeX source editor')).toHaveAttribute('spellcheck', 'false');
  await expect(page.getByTestId('latex-plugin-toolbar')).toHaveCount(0);
  await settings.locator('summary').click();
  await expect(page.getByTestId('latex-font-size')).toHaveValue('20');
});

test('Source caret movement, typing and line selection never remount the CodeMirror view', async ({ page }) => {
  const workspaceName = `source-input-regression-${Date.now()}`;
  await createBrowserWorkspace(page, { workspaceName });
  const source = page.getByLabel('LaTeX source editor');
  const editor = page.locator('[data-latex-codemirror] .cm-editor');
  await source.fill('alpha line\nbeta line\ngamma line\n');
  await editor.evaluate((element) => { (element as HTMLElement).dataset.sourceInputIdentity = 'stable'; });

  await source.press('ControlOrMeta+End');
  await page.keyboard.type('delta');
  await expect(source).toContainText('gammadelta');
  await expect(editor).toHaveAttribute('data-source-input-identity', 'stable');

  await source.press('ControlOrMeta+Home');
  await source.press('Shift+ArrowDown');
  await expect.poll(async () => source.evaluate(() => window.getSelection()?.toString().length ?? 0)).toBeGreaterThan(0);
  await expect(editor).toHaveAttribute('data-source-input-identity', 'stable');

  await page.keyboard.press('ArrowRight');
  await page.keyboard.type('!');
  await expect(editor).toHaveAttribute('data-source-input-identity', 'stable');
});

test('R14.19 source editor can enter and leave fullscreen without rebuilding the document', async ({ page, browserName }) => {
  test.skip(browserName === 'webkit', 'release certification targets Chromium/Edge and Firefox');
  const workspaceName = `fullscreen-r14-19-${Date.now()}`;
  await createBrowserWorkspace(page, { workspaceName });
  const editor = page.locator('[data-latex-codemirror] .cm-editor');
  await editor.evaluate((element) => { (element as HTMLElement).dataset.r1419FullscreenIdentity = 'same'; });
  await page.getByTestId('latex-fullscreen-toggle').click();
  await expect.poll(() => page.evaluate(() => Boolean(document.fullscreenElement))).toBe(true);
  await expect(editor).toHaveAttribute('data-r1419-fullscreen-identity', 'same');
  await page.getByTestId('latex-fullscreen-toggle').click();
  await expect.poll(() => page.evaluate(() => Boolean(document.fullscreenElement))).toBe(false);
  await expect(editor).toHaveAttribute('data-r1419-fullscreen-identity', 'same');
});

test('R31 manual Save confirms only after a durable source flush and exposes a close control', async ({ page }) => {
  const workspaceName = `source-save-r31-${Date.now()}`;
  await createBrowserWorkspace(page, { workspaceName });
  const source = page.getByLabel('LaTeX source editor');
  await source.fill('\\documentclass{article}\n\\begin{document}\nSaved by R31.\n\\end{document}\n');

  await page.getByRole('button', { name: 'Save', exact: true }).click();
  const toast = page.locator('[data-sonner-toast]').filter({ hasText: 'Saved' }).last();
  await expect(toast).toBeVisible();
  await expect(toast.locator('[data-close-button]')).toBeVisible();
  await toast.locator('[data-close-button]').click();

  await source.press('ControlOrMeta+End');
  await page.keyboard.type('% keyboard save');
  await source.press('ControlOrMeta+s');
  await expect(page.locator('[data-sonner-toast]').filter({ hasText: 'Saved' }).last()).toBeVisible();
});
