import { describe, expect, it } from 'vitest'

import {
  filterChain138Tokens,
  mergeTokenListLookups,
  TOKEN_LIST_SURFACE_LABELS,
} from './tokenListSurfaces'
import type { TokenListToken } from './config'

describe('tokenListSurfaces', () => {
  it('filters and sorts chain 138 tokens', () => {
    const tokens: TokenListToken[] = [
      { chainId: 138, address: '0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', symbol: 'cUSDC' },
      { chainId: 1, address: '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', symbol: 'USDC' },
      { chainId: 138, address: '0xcccccccccccccccccccccccccccccccccccccccc', symbol: 'cUSDT' },
    ]

    expect(filterChain138Tokens(tokens).map((token) => token.symbol)).toEqual(['cUSDC', 'cUSDT'])
  })

  it('prefers primary tokens when merging lookup maps', () => {
    const primary: TokenListToken[] = [
      { chainId: 138, address: '0x1111111111111111111111111111111111111111', symbol: 'cUSDT', tags: ['canonical'] },
    ]
    const secondary: TokenListToken[] = [
      { chainId: 138, address: '0x1111111111111111111111111111111111111111', symbol: 'cUSDT', tags: ['staged'] },
      { chainId: 138, address: '0x2222222222222222222222222222222222222222', symbol: 'cUSDC' },
    ]

    const lookup = mergeTokenListLookups(primary, secondary)
    expect(lookup.get('0x1111111111111111111111111111111111111111')?.tags).toEqual(['canonical'])
    expect(lookup.get('0x2222222222222222222222222222222222222222')?.symbol).toBe('cUSDC')
  })

  it('documents surface labels', () => {
    expect(TOKEN_LIST_SURFACE_LABELS.wallet).toMatch(/report/i)
    expect(TOKEN_LIST_SURFACE_LABELS.extended).toMatch(/catalog/i)
  })
})
