import { test } from 'node:test';
import assert from 'node:assert/strict';
import { Descendant } from 'slate';
import { toMatrixCustomHTML } from './output';
import { BlockType } from './types';

// Loose Slate node builders for the test.
const txt = (text: string, marks: Record<string, unknown> = {}) =>
  ({ text, ...marks }) as unknown as Descendant;
const el = (type: BlockType, children: unknown[]) => ({ type, children }) as unknown as Descendant;

const OPTS = {
  allowTextFormatting: true,
  allowInlineMarkdown: false,
  allowBlockMarkdown: false,
  allowMath: true,
};

test('inline $…$ → data-mx-maths span, surrounding prose preserved', () => {
  assert.equal(
    toMatrixCustomHTML(txt('a $x^2$ b'), OPTS),
    'a <span data-mx-maths="x^2"><code>x^2</code></span> b',
  );
});

test('single-line block $$…$$ → data-mx-maths div', () => {
  assert.equal(
    toMatrixCustomHTML(txt('$$E=mc^2$$'), OPTS),
    '<div data-mx-maths="E=mc^2"><code>E=mc^2</code></div>',
  );
});

test('LaTeX special chars are escaped in attribute + fallback', () => {
  assert.equal(
    toMatrixCustomHTML(txt('$a<b$'), OPTS),
    '<span data-mx-maths="a&lt;b"><code>a&lt;b</code></span>',
  );
});

test('math bypasses markdown — underscores are not italicised', () => {
  // Without pre-markdown extraction, `_` would become <em>.
  assert.equal(
    toMatrixCustomHTML(txt('$a_b$'), { ...OPTS, allowInlineMarkdown: true }),
    '<span data-mx-maths="a_b"><code>a_b</code></span>',
  );
});

test('currency ($5 and $10) stays literal', () => {
  assert.equal(toMatrixCustomHTML(txt('$5 and $10'), OPTS), '$5 and $10');
});

test('no math conversion inside an inline code mark', () => {
  const out = toMatrixCustomHTML(txt('$x$', { code: true }), OPTS);
  assert.ok(!out.includes('data-mx-maths'));
  assert.ok(out.includes('<code>$x$</code>'));
});

test('no math conversion inside a code block', () => {
  const block = el(BlockType.CodeBlock, [el(BlockType.CodeLine, [txt('$x$')])]);
  const out = toMatrixCustomHTML(block, OPTS);
  assert.ok(!out.includes('data-mx-maths'));
  assert.ok(out.includes('$x$'));
});

test('a message with no math is unchanged', () => {
  assert.equal(toMatrixCustomHTML(txt('just hello'), OPTS), 'just hello');
});
