###eslint-env mocha, es6 ###

import path from 'path'
import generate from '@gerhobbelt/markdown-it-testgen'
import assert from 'assert'
import markdownIt from '@gerhobbelt/markdown-it'

import plugin from '../index.js'



#-------------------------
# WARNING: manually mix in the __dirname, etc. patch as coffeescript screws that code up any way!
#-------------------------

import { fileURLToPath } from 'url';

# see https://nodejs.org/docs/latest-v13.x/api/esm.html#esm_no_require_exports_module_exports_filename_dirname
__filename = fileURLToPath(meta.url);   # import!
__dirname = path.dirname __filename

#-------------------------
# WARNING: manually mix in the __dirname, etc. patch as coffeescript screws that code up any way!
#-------------------------



describe 'markdown-it-checkbox', ->

  describe 'markdown-it-checkbox()', ->
    md = markdownIt()
    md.use plugin, {divWrap: false}
    generate path.join(__dirname, 'fixtures/checkbox.txt'), md

    it 'should pass irrelevant markdown', (done) ->
      res = md.render('# test')
      assert.strictEqual res.toString(), '<h1>test</h1>\n'
      done()

  describe 'markdown-it-checkbox(options)', ->
    it 'should optionally wrap arround a div layer', (done) ->
      md = markdownIt()
      md.use plugin, {divWrap: true}
      res = md.render('[X] test written')
      assert.strictEqual res.toString(), '<p>' +
        '<div class="checkbox">' +
        '<input type="checkbox" id="checkbox0" checked="">' +
        '<label for="checkbox0">test written</label>' +
        '</div>' +
        '</p>\n'
      done()

    it 'should optionally change class of div layer', (done) ->
      md = markdownIt()
      md.use plugin, {divWrap: true, divClass: 'cb'}
      res = md.render('[X] test written')
      assert.strictEqual res.toString(), '<p>' +
        '<div class="cb">' +
        '<input type="checkbox" id="checkbox0" checked="">' +
        '<label for="checkbox0">test written</label>' +
        '</div>' +
        '</p>\n'
      done()

    it 'should optionally change the id', (done) ->
      md = markdownIt()
      md.use plugin, {idPrefix: 'cb'}
      res = md.render('[X] test written')
      assert.strictEqual res.toString(), '<p>' +
        '<input type="checkbox" id="cb0" checked="">' +
        '<label for="cb0">test written</label>' +
        '</p>\n'
      done()

    it 'should optionally be readonly', (done) ->
      md = markdownIt()
      md.use plugin, {readonly: true}
      res = md.render('[X] test written')
      assert.strictEqual res.toString(), '<p>' +
        '<input type="checkbox" id="checkbox0" checked="" readonly="">' +
        '<label for="checkbox0">test written</label>' +
        '</p>\n'
      done()

  describe 'markdown-it-checkbox() with nested markdown', ->
    md = markdownIt()
    md.use plugin, {divWrap: false}

    it 'should encapsulate subsequent content', (done) ->
      res = md.render('[X] test *written* ~~in~~ ' +
                          '**markdown** and [followed]() with content')
      assert.strictEqual res.toString(), '<p>' +
        '<input type="checkbox" id="checkbox0" checked="">' +
        '<label for="checkbox0">test <em>written</em> <s>in</s> ' +
        '<strong>markdown</strong> and <a href="">followed</a> with content' +
        '</label></p>\n'
      done()

    it 'should should optionally add disabled attribute', (done) ->
      md = markdownIt()
      md.use plugin, {disabled: true}
      res = md.render('[X] test written')
      assert.strictEqual res.toString(), '<p>' +
        '<input type="checkbox" id="checkbox0" checked="" disabled="">' +
        '<label for="checkbox0">test written</label>' +
        '</p>\n'
      done()

    it 'should apply custom html', (done) ->
      md = markdownIt()
      md.use plugin, {disabled: true, customHTML: '
        <div class="checklist-item">\
          <div class="checklist-item__checkbox">\
            <input type="checkbox">\
          </div>\
           <label class="checklist-item__label"\
           >test written2</label>\
          <label class="checklist-item__label">\
          {label}\
          </label>\
        </div>'}
      res = md.render('[X] test written')
      assert.strictEqual res.toString(), '
        <p><div class="checklist-item">\
          <div class="checklist-item__checkbox">\
            <input disabled="" checked="" \
            id="checkbox0" type="checkbox">\
          </div>\
            <label class="checklist-item__label"\
            >test written2</label>\
            <label for="checkbox0" class="checklist-item__label"\
            >test written</label>\
        </div></p>\n'
      done()

    it 'custom html without label tag', (done) ->
      md = markdownIt()
      md.use plugin, {disabled: true, customHTML: '
        <div class="checklist-item">\
          <div class="checklist-item__checkbox">\
            <input type="checkbox">\
          </div>\
        </div>'}
      res = md.render('[X] test written')
      assert.strictEqual res.toString(), '
        <p><div class="checklist-item">\
          <div class="checklist-item__checkbox">\
            <input disabled="" checked="" \
            id="checkbox0" type="checkbox">\
          </div>\
        </div></p>\n'
      done()

    it 'custom html without input tag', (done) ->
      md = markdownIt()
      md.use plugin, {disabled: true, customHTML: '
      <div class="checklist-item">\
        <div class="checklist-item__checkbox">\
        </div>\
        <label class="checklist-item__label">\
        </label>\
      </div>'}
      res = md.render('[X] test written')
      assert.strictEqual res.toString(), '
      <p><div class="checklist-item">\
        <div class="checklist-item__checkbox">\
        </div>\
        <label for="checkbox0" class="checklist-item__label"\
        >test written</label>\
      </div></p>\n'
      done()
