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

describe('ApiRequestElement', () => {
  const store = new DomEventsAmfStore(window)
  store.listen()

  let loader: AmfLoader
  before(async () => {
    await loadMonaco()
    loader = new AmfLoader()
  })

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

  describe('serialize()', () => {
    let model: AmfDefinitions.IAmfDocument
    before(async () => {
      model = await loader.getGraph()
      store.amf = model
    })

    it('serializes the current request', async () => {
      const method = loader.lookupOperation(model, '/people', 'get') as AmfDefinitions.IAmfOperation
      const panel = await basicFixture(method['@id'])
      const result = await panel.serialize()
      assert.include(result.expects.url, '/people')
    })
  })
})
