import { test } from '@japa/runner'
import { AmfSerializer } from '../../../../src/amf/AmfSerializer.js'
import type { IAmfDocument } from '../../../../src/amf/definitions/Amf.js'
import type { IApiObjectNode, IApiScalarNode } from '../../../../src/amf/definitions/Shapes.js'
import { AmfLoader } from '../AmfLoader.js'

const loader = new AmfLoader()

test.group('Annotations', (group) => {
  let api: IAmfDocument
  let serializer: AmfSerializer
  group.setup(async () => {
    api = await loader.getGraph('amf-helper-api')
    serializer = new AmfSerializer()
    serializer.amf = api
  })

  test('ObjectNode annotations', ({ assert }) => {
    const shape = loader.lookupOperation(api, '/about', 'get')
    const result = serializer.operation(shape)
    assert.typeOf(result, 'object', 'returns an object')
    const { customDomainProperties } = result
    assert.typeOf(customDomainProperties, 'array', 'has the customDomainProperties')
    assert.lengthOf(customDomainProperties, 1, 'has a single annotation')
    const [annotation] = customDomainProperties
    assert.equal(annotation.name, 'clearanceLevel', 'has the extensionName')
    assert.typeOf(annotation.extension, 'object', 'has the extension')
    const { properties } = annotation.extension as IApiObjectNode
    assert.typeOf(properties, 'object', 'has properties')
    const { level, signature } = properties
    assert.typeOf(level, 'object', 'has the level property')
    assert.typeOf(signature, 'object', 'has the level property')
  })

  test('ScalarShape annotations', ({ assert }) => {
    const shape = loader.lookupOperation(api, '/files', 'post')
    const result = serializer.operation(shape)
    assert.typeOf(result, 'object', 'returns an object')
    const { customDomainProperties } = result
    assert.typeOf(customDomainProperties, 'array', 'has the customDomainProperties')
    assert.lengthOf(customDomainProperties, 1, 'has a single annotation')
    const [annotation] = customDomainProperties
    assert.equal(annotation.name, 'deprecated', 'has the extensionName')
    assert.typeOf(annotation.extension, 'object', 'has the extension')

    const { value, dataType } = annotation.extension as IApiScalarNode
    assert.equal(dataType, 'http://www.w3.org/2001/XMLSchema#string', 'has the dataType')
    assert.equal(value, 'This operation is deprecated and will be removed.', 'has the value')
  })
})
