/**
 * (parse Axios error body: Blob JSON, nested JSON string, description).
 */
import { AxiosError } from 'axios'
import { getAxiosErrorMessageAsync } from '../axios-error-message'

const fallback = 'default'

describe('[util/axios-error-message] getAxiosErrorMessageAsync', () => {
  describe('Blob response data', () => {
    it('prefers error field from parsed Blob JSON', async () => {
      const blob = new Blob([JSON.stringify({ error: 'blob-err' })], {
        type: 'application/json',
      })
      const err = { response: { data: blob } } as AxiosError<unknown>
      await expect(getAxiosErrorMessageAsync(err, fallback)).resolves.toBe(
        'blob-err',
      )
    })

    it('uses message when error field missing', async () => {
      const blob = new Blob([JSON.stringify({ message: 'blob-msg' })], {
        type: 'application/json',
      })
      const err = { response: { data: blob } } as AxiosError<unknown>
      await expect(getAxiosErrorMessageAsync(err, fallback)).resolves.toBe(
        'blob-msg',
      )
    })

    it('falls back when Blob is not valid JSON', async () => {
      const blob = new Blob(['not-json'], { type: 'text/plain' })
      const err = { response: { data: blob } } as AxiosError<unknown>
      await expect(getAxiosErrorMessageAsync(err, fallback)).resolves.toBe(
        fallback,
      )
    })
  })

  describe('plain object / nested error', () => {
    it('reads error from plain object body', async () => {
      const err = {
        response: { data: { error: 'plain' } },
      } as AxiosError<unknown>
      await expect(getAxiosErrorMessageAsync(err, fallback)).resolves.toBe(
        'plain',
      )
    })

    it('extracts description from JSON string in error field', async () => {
      const nested = JSON.stringify({
        code: 409,
        message: 'Resource Already Exists',
        description:
          'An application already exists with name ádsad',
        moreinfo: '',
        error: [],
      })
      const err = {
        response: {
          data: {
            code: '1004',
            message: 'APP_NAME_DUPLICATE',
            error: nested,
          },
        },
      } as AxiosError<unknown>
      await expect(getAxiosErrorMessageAsync(err, fallback)).resolves.toBe(
        'An application already exists with name ádsad',
      )
    })

    it('extracts description from object error field', async () => {
      const err = {
        response: {
          data: {
            code: '1004',
            message: 'APP_NAME_DUPLICATE',
            error: {
              description: 'Duplicate name',
            },
          },
        },
      } as AxiosError<unknown>
      await expect(getAxiosErrorMessageAsync(err, fallback)).resolves.toBe(
        'Duplicate name',
      )
    })
  })

  describe('errors — missing body', () => {
    it('returns fallback when no usable payload', async () => {
      const err = {} as AxiosError<unknown>
      await expect(getAxiosErrorMessageAsync(err, fallback)).resolves.toBe(
        fallback,
      )
    })
  })
})
