import { describe, expect, it } from 'vitest'
import {
  inferDirectSearchTarget,
  inferTokenSearchTarget,
  normalizeExplorerSearchResults,
  suggestCuratedTokens,
} from './search'

describe('inferDirectSearchTarget', () => {
  it('detects addresses and normalizes the prefix', () => {
    expect(
      inferDirectSearchTarget('0X1234567890123456789012345678901234567890'),
    ).toEqual({
      kind: 'address',
      href: '/addresses/0x1234567890123456789012345678901234567890',
      label: 'Open address',
    })
  })

  it('detects transaction hashes', () => {
    expect(
      inferDirectSearchTarget(
        '0x1234567890123456789012345678901234567890123456789012345678901234',
      ),
    ).toEqual({
      kind: 'transaction',
      href: '/transactions/0x1234567890123456789012345678901234567890123456789012345678901234',
      label: 'Open transaction',
    })
  })

  it('detects block numbers', () => {
    expect(inferDirectSearchTarget(' 12345 ')).toEqual({
      kind: 'block',
      href: '/blocks/12345',
      label: 'Open block',
    })
  })

  it('returns null for generic text', () => {
    expect(inferDirectSearchTarget('cUSDT')).toBeNull()
  })

  it('detects curated token symbols and addresses', () => {
    expect(inferTokenSearchTarget('cUSDT', [
      { chainId: 138, symbol: 'cUSDT', address: '0x93E66202A11B1772E55407B32B44e5Cd8eda7f22' },
    ])).toEqual({
      kind: 'token',
      href: '/tokens/0x93E66202A11B1772E55407B32B44e5Cd8eda7f22',
      label: 'Open token (cUSDT)',
    })

    expect(inferTokenSearchTarget('0x93e66202a11b1772e55407b32b44e5cd8eda7f22', [
      { chainId: 138, symbol: 'cUSDT', address: '0x93E66202A11B1772E55407B32B44e5Cd8eda7f22' },
    ])).toEqual({
      kind: 'token',
      href: '/tokens/0x93E66202A11B1772E55407B32B44e5Cd8eda7f22',
      label: 'Open token (cUSDT)',
    })
  })
})

describe('normalizeExplorerSearchResults', () => {
  it('keeps token metadata and prefers token identity over duplicate address results', () => {
    const results = normalizeExplorerSearchResults(
      '0x93E66202A11B1772E55407B32B44e5Cd8eda7f22',
      [
        {
          type: 'token',
          address: '0x93E66202A11B1772E55407B32B44e5Cd8eda7f22',
          name: 'Tether USD (Compliant)',
          symbol: 'cUSDT',
          token_type: 'ERC-20',
          priority: 2,
        },
        {
          type: 'address',
          address: '0x93E66202A11B1772E55407B32B44e5Cd8eda7f22',
          name: 'Tether USD (Compliant)',
          priority: 0,
        },
      ],
      [
        {
          chainId: 138,
          address: '0x93E66202A11B1772E55407B32B44e5Cd8eda7f22',
          symbol: 'cUSDT',
          name: 'Tether USD (Compliant)',
        },
      ],
    )

    expect(results).toHaveLength(1)
    expect(results[0]).toMatchObject({
      type: 'token',
      href: '/tokens/0x93E66202A11B1772E55407B32B44e5Cd8eda7f22',
      label: 'Token · cUSDT',
      name: 'Tether USD (Compliant)',
      symbol: 'cUSDT',
      token_type: 'ERC-20',
      is_curated_token: true,
      match_reason: 'exact curated token address',
    })
  })

  it('sorts exact token matches ahead of weaker results', () => {
    const results = normalizeExplorerSearchResults(
      'USDT',
      [
        {
          type: 'address',
          address: '0xabc0000000000000000000000000000000000000',
          name: 'Treasury address',
          priority: 5,
        },
        {
          type: 'token',
          address: '0x004b63A7B5b0E06f6bB6adb4a5F9f590BF3182D1',
          name: 'Tether USD (Chain 138)',
          symbol: 'USDT',
          token_type: 'ERC-20',
          priority: 2,
        },
      ],
    )

    expect(results[0]).toMatchObject({
      type: 'token',
      symbol: 'USDT',
      href: '/tokens/0x004b63A7B5b0E06f6bB6adb4a5F9f590BF3182D1',
    })
  })

  it('suggests curated tokens by partial symbol, name, or tag', () => {
    expect(
      suggestCuratedTokens('gold', [
        {
          chainId: 138,
          address: '0x1',
          symbol: 'cXAUC',
          name: 'Gold Reserve Unit',
          tags: ['listed', 'gold'],
        },
        {
          chainId: 138,
          address: '0x2',
          symbol: 'USDT',
          name: 'Tether USD',
          tags: ['listed'],
        },
      ]),
    ).toHaveLength(1)
  })
})
