import { expect } from 'chai';
import { MarkdownToHTML } from '../../../source/text/markdown-parser.mjs';

describe('MarkdownToHTML', () => {

    it('should convert headings correctly', () => {
        const md = '# Title\n## Subtitle';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.include('<h1>Title</h1>');
        expect(html).to.include('<h2>Subtitle</h2>');
    });

    it('should convert paragraphs correctly', () => {
        const md = 'This is a paragraph.\n\nAnother one.';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.include('<p>This is a paragraph.</p>');
        expect(html).to.include('<p>Another one.</p>');
    });

    it('should convert bold, italic and inline code', () => {
        const md = '**bold** *italic* `code`';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.include('<strong>bold</strong>');
        expect(html).to.include('<em>italic</em>');
        expect(html).to.include('<code>code</code>');
    });

    it('should convert links correctly', () => {
        const md = '[Link](https://example.com)';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.include('<a href="https://example.com">Link</a>');
    });

    it('should convert unordered lists', () => {
        const md = '- Item 1\n- Item 2';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.include('<ul>');
        expect(html).to.include('<li>Item 1</li>');
        expect(html).to.include('<li>Item 2</li>');
    });

    it('should convert ordered lists', () => {
        const md = '1. First\n2. Second';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.include('<ol>');
        expect(html).to.include('<li>First</li>');
        expect(html).to.include('<li>Second</li>');
    });

    it('should convert task lists with disabled checkboxes', () => {
        const md = '- [ ] Task A\n- [x] Task B';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.include('type="checkbox"');
        expect(html).to.include('disabled');
        expect(html).to.include('checked');
    });

    it('should generate unique checkbox IDs for task list items', () => {
        const md = '- [ ] A\n- [ ] B';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.match(/<input type="checkbox" id="mdtask-1"/);
        expect(html).to.match(/<input type="checkbox" id="mdtask-2"/);
    });

    it('should render code blocks with language class', () => {
        const md = '```js\nconst x = 1;\n```';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.include('<pre><code class="language-js">');
        expect(html).to.include('const x = 1;');
    });

    it('should handle open code blocks at EOF', () => {
        const md = '```python\nprint("Hi")';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.include('print("Hi")');
        expect(html).to.include('<code class="language-python">');
    });

    it('should escape HTML in paragraphs and code blocks by default', () => {
        const md = '<div>\n\n```html\n<div></div>\n```';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.include('&lt;div&gt;');
        expect(html).to.include('&lt;/div&gt;');
    });

    it('should disable HTML escaping when escapeHTML=false', () => {
        const md = '<span>';
        const html = MarkdownToHTML.convert(md, { escapeHTML: false });
        expect(html).to.include('<span>');
    });

    it('should parse inline markdown correctly inside list items', () => {
        const md = '- **bold** and *italic*';
        const html = MarkdownToHTML.convert(md);
        expect(html).to.include('<strong>bold</strong>');
        expect(html).to.include('<em>italic</em>');
    });

    it('should ignore empty input', () => {
        const html = MarkdownToHTML.convert('');
        expect(html).to.equal('');
    });

});
