import { describe, test, expect } from 'vitest'
import { sanitizeDataItem, sanitizeDataItems } from './serializer'
import type { DataItem } from './types'

describe('sanitizeDataItem', () => {
  test('preserves string prefix and suffix', () => {
    const item: DataItem = {
      value: 100,
      prefix: '$',
      suffix: 'USD',
      color: 'blue',
    }

    const result = sanitizeDataItem(item)

    expect(result).toEqual({
      value: 100,
      prefix: '$',
      suffix: 'USD',
      color: 'blue',
    })
  })

  test('converts ReactNode prefix to undefined', () => {
    const item: DataItem = {
      value: 100,
      prefix: <span>$</span>,
      suffix: 'USD',
    }

    const result = sanitizeDataItem(item)

    expect(result).toEqual({
      value: 100,
      prefix: undefined,
      suffix: 'USD',
    })
  })

  test('converts ReactNode suffix to undefined', () => {
    const item: DataItem = {
      value: 100,
      prefix: '$',
      suffix: <span>USD</span>,
    }

    const result = sanitizeDataItem(item)

    expect(result).toEqual({
      value: 100,
      prefix: '$',
      suffix: undefined,
    })
  })

  test('converts both ReactNode prefix and suffix to undefined', () => {
    const item: DataItem = {
      value: 100,
      prefix: <span>$</span>,
      suffix: <span>USD</span>,
      color: 'red',
    }

    const result = sanitizeDataItem(item)

    expect(result).toEqual({
      value: 100,
      prefix: undefined,
      suffix: undefined,
      color: 'red',
    })
  })

  test('handles missing prefix and suffix', () => {
    const item: DataItem = {
      value: 100,
    }

    const result = sanitizeDataItem(item)

    expect(result).toEqual({
      value: 100,
      prefix: undefined,
      suffix: undefined,
    })
  })

  test('preserves other properties', () => {
    const item: DataItem = {
      value: 42,
      color: '#FF5733',
    }

    const result = sanitizeDataItem(item)

    expect(result).toEqual({
      value: 42,
      prefix: undefined,
      suffix: undefined,
      color: '#FF5733',
    })
  })
})

describe('sanitizeDataItems', () => {
  test('sanitizes array of items', () => {
    const items: DataItem[] = [
      { value: 100, prefix: '$', suffix: 'USD' },
      { value: 200, prefix: <span>€</span>, suffix: 'EUR' },
      { value: 300, prefix: '¥', suffix: <span>JPY</span> },
    ]

    const result = sanitizeDataItems(items)

    expect(result).toEqual([
      { value: 100, prefix: '$', suffix: 'USD' },
      { value: 200, prefix: undefined, suffix: 'EUR' },
      { value: 300, prefix: '¥', suffix: undefined },
    ])
  })

  test('handles undefined input', () => {
    const result = sanitizeDataItems(undefined)

    expect(result).toBeUndefined()
  })

  test('handles empty array', () => {
    const result = sanitizeDataItems([])

    expect(result).toEqual([])
  })

  test('does not mutate original array', () => {
    const items: DataItem[] = [
      { value: 100, prefix: <span>$</span>, suffix: 'USD' },
    ]
    const original = [...items]

    sanitizeDataItems(items)

    expect(items).toEqual(original)
  })
})
