import { fixture, assert, 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-298', () => {
    const store = new DomEventsAmfStore(window)
    store.listen()
    const apiFile = 'APIC-298'

    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 methodId: string
    let loader: AmfLoader
    let amf: AmfDefinitions.IAmfDocument
    before(async () => {
      loader = new AmfLoader()
      amf = await loader.getGraph(apiFile)
      store.amf = amf
    })

    let element: ApiRequestEditorElement
    beforeEach(async () => {
      const method = loader.lookupOperation(amf, '/prescreens/{id}', 'get') as AmfDefinitions.IAmfOperation
      methodId = method['@id']
      element = await modelFixture(methodId)
    })

    it('computes pth uri parameters', () => {
      const params = element.amfParameters.parametersValue
      assert.lengthOf(params, 1, 'pathModel has no elements')
      const [param] = params
      assert.equal(param.binding, 'path', 'has a path item only')
    })

    it('has OAS property name', () => {
      const params = element.amfParameters.parametersValue
      const [param] = params
      assert.equal(param.parameter.paramName, 'id')
    })
  })
})
