import { assert, fixture } from '@open-wc/testing'
import { loadMonaco } from '../MonacoSetup.js'
import BodyTextEditor from '../../../src/elements/http/BodyTextEditor.js'
import '../../../src/define/http/http-body-text-editor.js'

/* global monaco */

describe('elements', () => {
  describe('http', () => {
    describe('BodyTextEditor()', () => {
      async function basicFixture(): Promise<BodyTextEditor> {
        return fixture(`<http-body-text-editor></http-body-text-editor>`)
      }

      before(async () => loadMonaco())

      describe('constructor()', () => {
        let element: BodyTextEditor
        beforeEach(async () => {
          element = await basicFixture()
        })

        it('sets the default readOnly', () => {
          assert.isFalse(element.readOnly)
        })

        it('sets the default value', () => {
          assert.equal(element.value, '')
        })
      })

      describe('#editor', () => {
        let element: BodyTextEditor
        beforeEach(async () => {
          element = await basicFixture()
        })

        it('has the editor instance set', () => {
          assert.typeOf(element.editor, 'object')
        })
      })

      describe('#readOnly', () => {
        let element: BodyTextEditor
        beforeEach(async () => {
          element = await basicFixture()
        })

        it('sets the editor readOnly', () => {
          element.readOnly = true
          // @ts-expect-error for testing
          const value = element.editor.getOption(monaco.editor.EditorOptions.readOnly.id)
          assert.isTrue(value)
        })
      })

      describe('#contentType', () => {
        let element: BodyTextEditor
        beforeEach(async () => {
          element = await basicFixture()
        })

        it('sets the [languageValue]', () => {
          element.contentType = 'application/json'
          assert.equal(element.language, 'json')
        })

        it('sets the editor model', () => {
          element.contentType = 'application/json'
          const model = element.editor!.getModel()!
          assert.equal(model.getLanguageId(), 'json')
        })
        ;[
          ['application/json', 'json'],
          ['application/x-json', 'json'],
          ['application/svg+xml', 'xml'],
          ['application/xml', 'xml'],
          ['text/html', 'html'],
          ['text/css', 'css'],
        ].forEach(([mime, lang]) => {
          it(`sets the editor language for ${mime}`, () => {
            element.contentType = mime
            const model = element.editor!.getModel()!
            assert.equal(model.getLanguageId(), lang)
          })
        })
      })
    })
  })
})
