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

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

test('BF3 Visual Enter remains blank-line canonical and typing-ready across headings paragraphs and lists', async ({ page }) => {
  const workspaceName = `visual-bf2-${Date.now()}`;
  await createBrowserWorkspace(page, { workspaceName });

  const source = page.getByLabel('LaTeX source editor');
  await source.fill([
    '\\documentclass{article}',
    '\\begin{document}',
    '\\section{Methods}',
    'Existing paragraph.',
    '\\begin{itemize}',
    '\\item First bullet',
    '\\end{itemize}',
    '\\begin{enumerate}',
    '\\item First number',
    '\\end{enumerate}',
    '\\end{document}',
    '',
  ].join('\n'));
  await source.press(saveShortcut);

  await page.getByRole('button', { name: 'Visual' }).click();
  const visual = page.locator('.interactive-latex-visual-editor');
  await expect(visual).toBeVisible();

  // Enter on a section title must exit the heading through canonical source,
  // never through an unmapped ProseMirror-only paragraph.
  const heading = visual.locator('.interactive-visual-heading').filter({ hasText: 'Methods' }).first();
  await heading.click();
  await page.keyboard.press('End');
  await page.keyboard.press('Enter');
  await page.keyboard.type('Heading paragraph.');
  await expect(visual).toContainText('Heading paragraph.');

  // Repeated paragraph Enter must continue indefinitely. The next keystroke,
  // selection/delete, and slash menu must all remain operational.
  const headingParagraph = visual.locator('.interactive-visual-paragraph').filter({ hasText: 'Heading paragraph.' }).first();
  await headingParagraph.click();
  await page.keyboard.press('End');
  await page.keyboard.press('Enter');
  await page.keyboard.press('Enter');
  await page.keyboard.type('Delete me');
  await page.keyboard.press('Shift+Home');
  await page.keyboard.press('Backspace');
  await page.keyboard.type('/newline');
  await expect(page.getByTestId('latex-visual-slash-menu')).toBeVisible();
  await page.keyboard.press('Enter');
  await page.keyboard.type('After slash.');
  await expect(visual).toContainText('After slash.');

  // New item must be immediately editable; this is stronger than merely
  // checking that a second structural <li> appeared.
  const bullet = visual.locator('.interactive-visual-list-item').filter({ hasText: 'First bullet' }).first();
  await bullet.click();
  await page.keyboard.press('End');
  await page.keyboard.press('Enter');
  await page.keyboard.type('Second bullet');
  await expect(visual.locator('.interactive-visual-list-item').filter({ hasText: 'Second bullet' })).toBeVisible();

  const numbered = visual.locator('.interactive-visual-list-item').filter({ hasText: 'First number' }).first();
  await numbered.click();
  await page.keyboard.press('End');
  await page.keyboard.press('Enter');
  await page.keyboard.type('Second number');
  await expect(visual.locator('.interactive-visual-list-item').filter({ hasText: 'Second number' })).toBeVisible();

  await page.getByRole('button', { name: 'Source' }).click();
  await expect(source).toContainText('\\section{Methods}\n\nHeading paragraph.');
  await expect(source).toContainText('Heading paragraph.\n\n\n\\newline{}After slash.');
  await expect(source).not.toContainText('\\par');
  await expect(source).toContainText('\\item First bullet\n\\item Second bullet');
  await expect(source).toContainText('\\item First number\n\\item Second number');
  await expect(source).not.toContainText('Delete me');
});
