import { fixture, assert, html, aTimeout } from '@open-wc/testing'
import { ApiDefinitions, AmfDefinitions } from '@api-client/core/browser.js'
import { AmfLoader } from '../AmfLoader.js'
import { DomEventsAmfStore } from '../../../src/amf/store/DomEventsAmfStore.js'
import ApiOperationDocumentElement from '../../../src/elements/amf/ApiOperationDocument.element.js'
import ApiParameterDocumentElement from '../../../src/elements/amf/ApiParameterDocument.element.js'
import '../../../src/define/amf/api-operation-document.js'

// In OAS path parameters can be defined on an operation level. This is in conflict
// with RAML which only allows variables on the endpoint level.
// This tests for reading parameters from an endpoint that are locates on the
// method level.
describe('ApiOperationDocumentElement', () => {
  describe('SE-12957', () => {
    const loader = new AmfLoader()
    const store = new DomEventsAmfStore(window)
    store.listen()
    const apiFile = 'SE-12957'

    async function basicFixture(shape: ApiDefinitions.IApiOperation): Promise<ApiOperationDocumentElement> {
      const element = await fixture(
        html`<api-operation-document
          .queryDebouncerTimeout="${0}"
          .domainId="${shape && shape.id}"
          .operation="${shape}"
        ></api-operation-document>`
      )
      await aTimeout(0)
      return element as ApiOperationDocumentElement
    }

    let model: AmfDefinitions.IAmfDocument
    before(async () => {
      model = await loader.getGraph(apiFile)
    })

    it('computes path parameters from AMF Endpoint model', async () => {
      const data = loader.getOperation(model, '/api/v1/alarm/{scada-object-key}', 'get')
      const element = await basicFixture(data)
      const request = element.shadowRoot!.querySelector('api-request-document')!
      const params = request.shadowRoot!.querySelectorAll(
        'api-parameter-document[data-name="uri"]'
      ) as NodeListOf<ApiParameterDocumentElement>
      assert.lengthOf(params, 1, 'has a single path parameter')
      const param = params[0]
      assert.equal(param.parameter!.name, 'scada-object-key', 'has the model defined parameter')
    })
  })
})
