/**
 * Tests for serializeLexical utility
 */

import { describe, it, expect } from 'vitest'
import { serializeLexical } from './serializeLexical'

describe('serializeLexical', () => {
  it('should return empty string for null input', async () => {
    const result = await serializeLexical(null)
    expect(result).toBe('')
  })

  it('should return empty string for undefined input', async () => {
    const result = await serializeLexical(undefined)
    expect(result).toBe('')
  })

  it('should return empty string for empty string', async () => {
    const result = await serializeLexical('')
    expect(result).toBe('')
  })

  it('should return plain text as-is', async () => {
    const result = await serializeLexical('Hello world')
    expect(result).toBe('Hello world')
  })

  it('should return HTML as-is', async () => {
    const html = '<p>Hello <strong>world</strong></p>'
    const result = await serializeLexical(html)
    expect(result).toBe(html)
  })

  it('should convert Lexical JSON to HTML - simple paragraph', async () => {
    const lexicalData = {
      root: {
        type: 'root',
        children: [
          {
            type: 'paragraph',
            children: [
              {
                type: 'text',
                text: 'Hello world'
              }
            ]
          }
        ]
      }
    }

    const result = await serializeLexical(lexicalData)
    expect(result).toContain('Hello world')
    expect(result).toContain('<p>')
    expect(result).toContain('</p>')
  })

  it('should convert Lexical JSON with heading', async () => {
    const lexicalData = {
      root: {
        type: 'root',
        children: [
          {
            type: 'heading',
            tag: 'h2',
            children: [
              {
                type: 'text',
                text: 'Title'
              }
            ]
          }
        ]
      }
    }

    const result = await serializeLexical(lexicalData)
    expect(result).toContain('<h2>')
    expect(result).toContain('Title')
    expect(result).toContain('</h2>')
  })

  it('should convert Lexical JSON with bold text', async () => {
    const lexicalData = {
      root: {
        type: 'root',
        children: [
          {
            type: 'paragraph',
            children: [
              {
                type: 'text',
                text: 'Hello',
                bold: true
              }
            ]
          }
        ]
      }
    }

    const result = await serializeLexical(lexicalData)
    expect(result).toContain('<strong>')
    expect(result).toContain('Hello')
    expect(result).toContain('</strong>')
  })

  it('should convert Lexical JSON with link', async () => {
    const lexicalData = {
      root: {
        type: 'root',
        children: [
          {
            type: 'paragraph',
            children: [
              {
                type: 'link',
                url: 'https://example.com',
                children: [
                  {
                    type: 'text',
                    text: 'Click here'
                  }
                ]
              }
            ]
          }
        ]
      }
    }

    const result = await serializeLexical(lexicalData)
    expect(result).toContain('<a href="https://example.com"')
    expect(result).toContain('Click here')
    expect(result).toContain('</a>')
  })

  it('should handle stringified Lexical JSON', async () => {
    const lexicalString = JSON.stringify({
      root: {
        type: 'root',
        children: [
          {
            type: 'paragraph',
            children: [
              {
                type: 'text',
                text: 'Test content'
              }
            ]
          }
        ]
      }
    })

    const result = await serializeLexical(lexicalString)
    expect(result).toContain('Test content')
  })

  it('should handle array input gracefully', async () => {
    const result = await serializeLexical(['not', 'valid'])
    expect(result).toBe('')
  })

  it('should convert list nodes', async () => {
    const lexicalData = {
      root: {
        type: 'root',
        children: [
          {
            type: 'list',
            listType: 'bullet',
            children: [
              {
                type: 'listitem',
                children: [
                  {
                    type: 'text',
                    text: 'Item 1'
                  }
                ]
              }
            ]
          }
        ]
      }
    }

    const result = await serializeLexical(lexicalData)
    expect(result).toContain('<ul>')
    expect(result).toContain('<li>')
    expect(result).toContain('Item 1')
    expect(result).toContain('</li>')
    expect(result).toContain('</ul>')
  })

  it('should handle blockquote', async () => {
    const lexicalData = {
      root: {
        type: 'root',
        children: [
          {
            type: 'quote',
            children: [
              {
                type: 'text',
                text: 'Quote text'
              }
            ]
          }
        ]
      }
    }

    const result = await serializeLexical(lexicalData)
    expect(result).toContain('<blockquote>')
    expect(result).toContain('Quote text')
    expect(result).toContain('</blockquote>')
  })
})
