declare let hljs;
declare let SimpleMarkdown;

describe('SimpleMarkdown', () => {

    let parser = SimpleMarkdown.defaultBlockParse;
    let viewer = SimpleMarkdown.defaultHtmlOutput;


    // ---------------------------------------------------------------
    // simple "is it working" positive tests

    it('SimpleMarkdown installed', () => {
        expect(SimpleMarkdown).toBeTruthy();
    });

    // syntax tree tests
    it('should recognize text pharagraph', () => {
      let syntaxTree = parser('text');

      expect(syntaxTree[0]['type']).toEqual('paragraph');
      expect(syntaxTree[0]['content'][0]['content']).toEqual('text');
    });

    it('should recognize inline code with `code`', () => {
      let syntaxTree = parser('`code`');

      expect(syntaxTree[0]['content'][0]['type']).toEqual('inlineCode');
      expect(syntaxTree[0]['content'][0]['content']).toEqual('code');
    });

    it('should recognize emoji', () => {
      let syntaxTree = parser('😀');

      expect(syntaxTree[0]['content'][0]['type']).toEqual('emoji');
      expect(syntaxTree[0]['content'][0]['content'][0]).toEqual('😀');
    });

    it('should recognize escape character (\\)', () => {
      let syntaxTree = parser('\\\\');

      expect(syntaxTree[0]['content'][0]['type']).toEqual('text');
      expect(syntaxTree[0]['content'][0]['content']).toEqual('\\');
    });

    let similarTests = [
      {describe: 'strong', content: '**text**', type: 'strong', output: 'text'},
      {describe: 'underline', content: '__text__', type: 'u', output: 'text'},
      {describe: 'strikethrough', content: '~~text~~', type: 'del', output: 'text'},
      {describe: 'url, format: www.domain.hu', content: 'www.domain.hu', type: 'link', output: 'www.domain.hu'},
      {describe: 'url, format: http://domain.hu', content: 'http://domain.hu', type: 'link', output: 'http://domain.hu'},
      {describe: 'url, format: https://domain.hu', content: 'https://domain.hu', type: 'link', output: 'https://domain.hu'},
      {describe: 'url, format: http://www.domain.hu', content: 'http://www.domain.hu', type: 'link', output: 'http://www.domain.hu'},
      {describe: 'url, format: https://www.domain.hu', content: 'https://www.domain.hu', type: 'link', output: 'https://www.domain.hu'},
    ];

    similarTests.forEach((test)=>{
      it('should recognize '+test.describe, () => {
        let syntaxTree = parser(test.content);

        expect(syntaxTree[0]['content'][0]['type']).toEqual(test.type);
        expect(syntaxTree[0]['content'][0]['content'][0]['content']).toEqual(test.output);
      });
    }); 
    
    it('should recognize multi line code block', () => {
      let syntaxTree = parser(`\`\`\`css
      code
      \`\`\``);

      expect(syntaxTree[0]['type']).toEqual('codeBlock');
      expect(syntaxTree[0]['lang']).toEqual('css');
    });

    it('should recognize quote (started with ">")', () => {
      let syntaxTree = parser('> text');

      expect(syntaxTree[0]['type']).toEqual('blockQuote');
      expect(syntaxTree[0]['content'][0]['content'][0]['content']).toEqual('text');
    });

    // html converter tests

    it('should generate paragraph tag', () => {
      let html = viewer(parser('text'));
      expect(html).toEqual("<div class=\"paragraph\">text</div><br>");
    });

    it('should generate strong tag', () => {
      let html = viewer(parser('**strong**'));
      expect(html).toEqual("<div class=\"paragraph\"><strong>strong</strong></div><br>");
    });

    it('should generate underline tag', () => {
      let html = viewer(parser('__underline__'));
      expect(html).toEqual("<div class=\"paragraph\"><u>underline</u></div><br>");
    });

    it('should generate del tag', () => {
      let html = viewer(parser('~~strikethrough~~'));
      expect(html).toEqual("<div class=\"paragraph\"><del>strikethrough</del></div><br>");
    });
    
    it('should generate code tag for inline code', () => {
      let html = viewer(parser('`code`'));
      expect(html).toEqual("<div class=\"paragraph\"><code>code</code></div><br>");
    });

    it('should generate blockquote tag', () => {
      let html = viewer(parser('> quote'));
      expect(html).toEqual("<blockquote><div class=\"paragraph\">quote</div><br></blockquote>");
    });

    it('should generate a-link tag', () => {
      let html = viewer(parser('www.domain.hu'));
      expect(html).toEqual("<div class=\"paragraph\"><a href=\"www.domain.hu\" title=\"www.domain.hu\">www.domain.hu</a></div><br>");
    });

    it('should generate img for emoji', () => {
      let html = viewer(parser('😀'));
      expect(html).toEqual("<div class=\"paragraph\"><img class=\"emoji\" draggable=\"false\" alt=\"😀\" src=\"assets/img/twemoji/13.0.1/72x72/1f600.png\"/></div><br>");
    });

    it('should generate multiline code block tag', () => {
      let html = viewer(parser(`\`\`\`css
      code
      \`\`\``));

      expect(html).toEqual(`<pre><code class=\"language-css\">      code
      </code></pre>`);
    });

    it('should merge other token inside a token', () => {
      let html = viewer(parser("~~**__test__**~~"));

      expect(html).toEqual(`<div class=\"paragraph\"><del><strong><u>test</u></strong></del></div><br>`);
    });

    // negative tests
    it('should not generate token inside a multiline codeblock', () => {
      let html = viewer(parser(`\`\`\`css
      **code**
      \`\`\``));

      expect(html).toEqual(`<pre><code class=\"language-css\">      **code**
      </code></pre>`);
    });

    it('should not generate token inside a code tag for inline code', () => {
      let html = viewer(parser('`**code**`'));
      expect(html).toEqual("<div class=\"paragraph\"><code>**code**</code></div><br>");
    });

    

});
