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

test.group('Source maps', (group) => {
  const loader = new AmfLoader()
  const jsonMime = 'application/json'
  // const xmlMime = 'application/xml';

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

  test('adds parsedJsonSchema', ({ assert }) => {
    const shape = loader.lookupRequestPayload(api, '/json', 'post', jsonMime)
    // @ts-expect-error Used in testing
    let schema = shape[serializer._getAmfKey(AmfNamespace.aml.vocabularies.shapes.schema)]
    if (Array.isArray(schema)) {
      ;[schema] = schema
    }
    const result = serializer.unknownShape(schema)

    const { parsedJsonSchema } = result.sourceMaps!
    assert.typeOf(parsedJsonSchema, 'object', 'has parsedJsonSchema')
    assert.typeOf(parsedJsonSchema!.id, 'string', 'has the id')
    assert.typeOf(parsedJsonSchema!.element, 'string', 'has the element')
    assert.typeOf(parsedJsonSchema!.value, 'string', 'has the value')
  })

  test('adds autoGeneratedName', ({ assert }) => {
    const shape = loader.lookupRequestPayload(api, '/json', 'post', jsonMime)
    // @ts-expect-error Used in testing
    let schema = shape[serializer._getAmfKey(AmfNamespace.aml.vocabularies.shapes.schema)]
    if (Array.isArray(schema)) {
      ;[schema] = schema
    }
    const result = serializer.unknownShape(schema)

    const { autoGeneratedName } = result.sourceMaps!
    assert.typeOf(autoGeneratedName, 'array', 'has autoGeneratedName array')

    const [item] = autoGeneratedName!
    assert.typeOf(item.id, 'string', 'has the id')
    assert.typeOf(item.element, 'string', 'has the element')
    assert.typeOf(item.value, 'string', 'has the value')
  })
})
