import { test } from '@japa/runner'
import { AmfLoader } from '../AmfLoader.js'
import { AmfSerializer } from '../../../../src/amf/AmfSerializer.js'
import type { IAmfDocument } from '../../../../src/amf/definitions/Amf.js'
import type { IApiNodeShape, IApiTupleShape } from '../../../../src/amf/definitions/Shapes.js'
import { AmfNamespace } from '../../../../src/amf/definitions/Namespace.js'

test.group('APIC-483', (group) => {
  const fileName = 'APIC-483'
  const loader = new AmfLoader()

  let api: IAmfDocument
  let serializer: AmfSerializer
  group.setup(async () => {
    api = await loader.getGraph(fileName)
    serializer = new AmfSerializer()
    serializer.amf = api
  })

  test('serializes a TupleShape', ({ assert }) => {
    const expects = loader.lookupExpects(api, '/banks', 'post')
    // @ts-expect-error Used in testing
    const payload = expects[loader._getAmfKey(AmfNamespace.aml.vocabularies.apiContract.payload)][0]
    const shape = payload[serializer._getAmfKey(AmfNamespace.aml.vocabularies.shapes.schema)][0]
    const result = serializer.unknownShape(shape) as IApiNodeShape
    const { examples, properties } = result

    // serializer recognizes that the this example is referenced to a payload and not to a type.
    assert.lengthOf(examples, 0, 'has the examples')
    assert.lengthOf(properties, 1, 'has the properties')
    const array = properties[0].range as IApiTupleShape
    const { types, items } = array
    assert.include(types, AmfNamespace.aml.vocabularies.shapes.TupleShape, 'range has the TupleShape')
    assert.lengthOf(items, 1, 'range has the items')
  })
})
