import { test } from '@japa/runner'
import { ApiSchemaGenerator } from '../../../src/amf/ApiSchemaGenerator.js'
import { IAmfDocument } from '../../../src/amf/definitions/Amf.js'
import { IApiAnyShape } from '../../../src/amf/definitions/Shapes.js'
import { AmfLoader } from './AmfLoader.js'

test.group('SE-22063', (group) => {
  const loader = new AmfLoader()
  const xmlMime = 'application/xml'
  const apiFile = 'SE-22063'
  let model: IAmfDocument

  group.setup(async () => {
    model = await loader.getGraph(apiFile)
  })

  test('generates xml example using xml name and xml wrap', ({ assert }) => {
    const payload = loader.getPayloads(model, '/demo', 'post')[0]

    const anyShape = payload.schema as IApiAnyShape
    const result = ApiSchemaGenerator.asExample(anyShape, xmlMime, {
      renderExamples: true,
      renderOptional: true,
    })!

    assert.typeOf(result, 'object', 'generates the example')
    assert.typeOf(result.renderValue, 'string', 'has the value')

    assert.equal(
      result.renderValue,
      '<StockBalance>\n' +
        '  <type>site</type>\n' +
        '  <item>\n' +
        '    <StockLine>\n' +
        '      <ssccNumber>P19227</ssccNumber>\n' +
        '      <materialReference>CL54B</materialReference>\n' +
        '      <variantId>R0029</variantId>\n' +
        '      <batch>BA02931</batch>\n' +
        '      <quantity>1</quantity>\n' +
        '    </StockLine>\n' +
        '  </item>\n' +
        '</StockBalance>'
    )
  })
})
