// @vitest-environment happy-dom
import { CompletionContext } from '@codemirror/autocomplete';
import { EditorState } from '@codemirror/state';
import { describe, expect, it } from 'vitest';
import {
  createLatexCodeMirror,
  createLatexProjectCompletionSource,
  detectLatexNavigationTarget,
} from '../index';

describe('R14.19 Learnitive editor feature parity', () => {
  it('offers project citations, references and paths from one completion source', async () => {
    const source = createLatexProjectCompletionSource(async () => ({
      citations: [{ key: 'knuth1984', title: 'The TeXbook' }],
      labels: [{ label: 'sec:intro', detail: 'chapters/intro.tex:4' }],
      files: [{ path: 'figures/diagram.pdf' }, { path: 'chapters/intro.tex' }],
    }));

    const complete = async (doc: string) => {
      const state = EditorState.create({ doc });
      return source(new CompletionContext(state, doc.length, false));
    };

    expect((await complete('\\cite{knu'))?.options.map((item) => item.label)).toContain('knuth1984');
    expect((await complete('\\ref{sec:'))?.options.map((item) => item.label)).toContain('sec:intro');
    expect((await complete('\\includegraphics{fig'))?.options.map((item) => item.label)).toContain('figures/diagram.pdf');
  });

  it('detects Learnitive Ctrl/Cmd-click navigation targets without moving the caret itself', () => {
    const doc = String.raw`See \\ref{sec:intro}, \\cite{knuth1984} and \\input{chapters/intro}.`;
    const refPos = doc.indexOf('sec:intro') + 2;
    const citePos = doc.indexOf('knuth1984') + 2;
    const filePos = doc.indexOf('chapters/intro') + 3;
    expect(detectLatexNavigationTarget(doc, refPos)).toEqual({ kind: 'reference', target: 'sec:intro' });
    expect(detectLatexNavigationTarget(doc, citePos)).toEqual({ kind: 'citation', target: 'knuth1984' });
    expect(detectLatexNavigationTarget(doc, filePos)).toEqual({ kind: 'file', command: 'input', target: 'chapters/intro' });
  });

  it('reconfigures line numbers, font size and spellcheck on the same EditorView', () => {
    const parent = document.createElement('div');
    document.body.append(parent);
    const session = createLatexCodeMirror({
      doc: '\\section{Parity}',
      parent,
      ariaLabel: 'R14.19 editor',
      showLineNumbers: true,
      fontSize: 14,
      spellcheck: true,
      onSave: () => {},
      onUndo: () => {},
      onRedo: () => {},
    });
    const identity = session.view;
    expect(parent.querySelector('.cm-lineNumbers')).not.toBeNull();
    expect(session.view.contentDOM.getAttribute('spellcheck')).toBe('true');

    session.setLineNumbers(false);
    session.setFontSize(20);
    session.setSpellcheck(false);

    expect(session.view).toBe(identity);
    expect(parent.querySelector('.cm-lineNumbers')).toBeNull();
    expect(session.view.contentDOM.getAttribute('spellcheck')).toBe('false');
    expect(getComputedStyle(session.view.contentDOM).fontSize).toBe('20px');
    session.destroy();
    parent.remove();
  });
});
