import { fixture, assert, html, aTimeout } from '@open-wc/testing'
import sinon from 'sinon'
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.skip('ApiRequestEditorElement', () => {
  describe('SE-12042', () => {
    const store = new DomEventsAmfStore(window)
    store.listen()

    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
    }

    const apiFile = 'SE-12042'
    describe('http method computation', () => {
      let loader: AmfLoader
      let amf: AmfDefinitions.IAmfDocument
      before(async () => {
        loader = new AmfLoader()
        amf = await loader.getGraph(apiFile)
        store.amf = amf
      })

      it('sets headers from the authorization method', async () => {
        const method = loader.lookupOperation(amf, '/check/api-status', 'get') as AmfDefinitions.IAmfOperation
        const element = await modelFixture(method['@id'])
        await aTimeout(10)
        const spy = sinon.spy()
        element.addEventListener('api-request', spy)
        element.execute()
        const { detail } = spy.args[0][0]
        const { headers } = detail
        assert.equal(headers, 'Client-Id: 283a6722121141feb7a929793d5c\nClient-Secret: 1421b7a929793d51fe283a67221c')
      })

      it('sets query parameter from the authorization method', async () => {
        const method = loader.lookupOperation(amf, '/check/api-status', 'get') as AmfDefinitions.IAmfOperation
        const element = await modelFixture(method['@id'])
        await aTimeout(0)
        const spy = sinon.spy()
        element.addEventListener('api-request', spy)
        element.execute()
        const { detail } = spy.args[0][0]
        const { url } = detail

        assert.include(url, 'api-status?testParam=x-test-value')
      })
    })
  })
})
