import { normalizeAccountFields } from 'src/lib/profile/accountFields'

describe('normalizeAccountFields', () => {
  test('null/undefined account → empty, unverified', () => {
    expect(normalizeAccountFields(null)).toEqual({ fields: [], hasVerifiedField: false })
    expect(normalizeAccountFields(undefined)).toEqual({
      fields: [],
      hasVerifiedField: false,
    })
  })

  test('missing fields key → empty', () => {
    expect(normalizeAccountFields({})).toEqual({ fields: [], hasVerifiedField: false })
  })

  test('empty fields array (Pixelfed) → empty, unverified', () => {
    expect(normalizeAccountFields({ fields: [] })).toEqual({
      fields: [],
      hasVerifiedField: false,
    })
  })

  test('drops rows where both name and value are blank/whitespace', () => {
    const result = normalizeAccountFields({
      fields: [
        { name: '   ', value: '  ', verified_at: null },
        { name: '', value: '', verified_at: null },
      ],
    })
    expect(result).toEqual({ fields: [], hasVerifiedField: false })
  })

  test('keeps a label-only row', () => {
    const result = normalizeAccountFields({
      fields: [{ name: 'Pronouns', value: '', verified_at: null }],
    })
    expect(result.fields).toEqual([
      { name: 'Pronouns', valueHtml: '', verified: false },
    ])
  })

  test('keeps a value-only row', () => {
    const result = normalizeAccountFields({
      fields: [{ name: '', value: 'they/them', verified_at: null }],
    })
    expect(result.fields).toEqual([
      { name: '', valueHtml: 'they/them', verified: false },
    ])
  })

  test('verified_at set → verified + hasVerifiedField', () => {
    const result = normalizeAccountFields({
      fields: [
        {
          name: 'Website',
          value: '<a href="https://shork.xyz" rel="me">shork.xyz</a>',
          verified_at: '2024-01-01T00:00:00.000Z',
        },
      ],
    })
    expect(result.fields[0].verified).toBe(true)
    expect(result.hasVerifiedField).toBe(true)
  })

  test('verified_at null or empty string → not verified', () => {
    const result = normalizeAccountFields({
      fields: [
        { name: 'A', value: 'a', verified_at: null },
        { name: 'B', value: 'b', verified_at: '' },
      ],
    })
    expect(result.fields.map((f) => f.verified)).toEqual([false, false])
    expect(result.hasVerifiedField).toBe(false)
  })

  test('mixed verified/unverified preserves order and sets hasVerifiedField', () => {
    const result = normalizeAccountFields({
      fields: [
        { name: 'Pronouns', value: 'they/them', verified_at: null },
        { name: 'Website', value: 'x', verified_at: '2024-05-01T12:00:00Z' },
        { name: 'Location', value: 'Berlin', verified_at: null },
      ],
    })
    expect(result.fields.map((f) => f.name)).toEqual([
      'Pronouns',
      'Website',
      'Location',
    ])
    expect(result.fields.map((f) => f.verified)).toEqual([false, true, false])
    expect(result.hasVerifiedField).toBe(true)
  })

  test('value HTML is carried through unparsed and untrimmed', () => {
    const value =
      '<a href="https://shork.xyz" rel="me"><span class="invisible">https://</span><span>shork.xyz</span></a>'
    const result = normalizeAccountFields({
      fields: [{ name: 'Website', value, verified_at: null }],
    })
    expect(result.fields[0].valueHtml).toBe(value)
  })

  test('trims the name but leaves value HTML intact', () => {
    const result = normalizeAccountFields({
      fields: [{ name: '  Website  ', value: '  spaced  ', verified_at: null }],
    })
    expect(result.fields[0].name).toBe('Website')
    expect(result.fields[0].valueHtml).toBe('  spaced  ')
  })

  test('skips null/undefined entries defensively', () => {
    const result = normalizeAccountFields({
      // @ts-expect-error — exercising a malformed payload
      fields: [null, { name: 'Ok', value: 'v', verified_at: null }, undefined],
    })
    expect(result.fields).toEqual([{ name: 'Ok', valueHtml: 'v', verified: false }])
  })
})
