import { assert, fixture, html, aTimeout } from '@open-wc/testing'
import { AmfDefinitions } from '@api-client/core/browser.js'
import { AmfLoader } from '../AmfLoader.js'
import { DomEventsAmfStore } from '../../../src/index.js'
import ApiRequestEditorElement from '../../../src/elements/amf/ApiRequestEditor.element.js'
import '../../../src/define/amf/api-request-editor.js'

describe('ApiRequestEditorElement', () => {
  describe('APIC-689', () => {
    const store = new DomEventsAmfStore(window)
    store.listen()
    const apiFile = 'APIC-689'

    async function modelFixture(domainId: string): Promise<ApiRequestEditorElement> {
      const element = await fixture(html`<api-request-editor .domainId="${domainId}"></api-request-editor>`)
      await aTimeout(2)
      return element as ApiRequestEditorElement
    }

    let loader: AmfLoader
    let amf: AmfDefinitions.IAmfDocument

    before(async () => {
      loader = new AmfLoader()
      amf = await loader.getGraph(apiFile)
      store.amf = amf
    })

    it('does not set URL query param for an optional enum', async () => {
      const method = loader.lookupOperation(amf, '/test', 'get') as AmfDefinitions.IAmfOperation
      const methodId = method['@id']
      const editor = await modelFixture(methodId)
      const values = await editor.serialize()
      assert.equal(values.expects.url, '/test', 'param value is not set')
    })

    it('sets URL query param for a required enum', async () => {
      const method = loader.lookupOperation(amf, '/test', 'post') as AmfDefinitions.IAmfOperation
      const methodId = method['@id']
      const editor = await modelFixture(methodId)
      const values = await editor.serialize()
      assert.equal(values.expects.url, '/test?param1=A', 'param value is set')
    })
  })
})
