import { NATIVE_CHAIN_ID } from 'constants/tokens'
import { searchTokenToTokenSearchResult } from 'lib/utils/searchBar'
import { getNativeAddress } from 'uniswap/src/constants/addresses'
import {
  Chain,
  SafetyLevel,
  TokenStandard,
} from 'uniswap/src/data/graphql/uniswap-data-api/__generated__/types-and-hooks'
import { SearchResultType } from 'uniswap/src/features/search/SearchResult'
import { UniverseChainId } from 'uniswap/src/types/chains'

describe('searchBar', () => {
  describe('searchTokenToTokenSearchResult', () => {
    describe(`${NATIVE_CHAIN_ID}`, () => {
      it('accepts a searchToken and returns a TokenSearchResult', () => {
        const ethSearchResult = {
          type: SearchResultType.Token,
          chainId: UniverseChainId.Mainnet,
          address: null,
          logoUrl: 'eth-logo.png',
          symbol: 'ETH',
          name: 'Ethereum',
          safetyLevel: SafetyLevel.Verified,
        }

        expect(
          searchTokenToTokenSearchResult({
            decimals: 18,
            name: 'Ethereum',
            chain: Chain.Ethereum,
            // This is not a mistake, sometimes the standard for ETH is ERC20
            // in search results.
            standard: TokenStandard.Erc20,
            address: NATIVE_CHAIN_ID,
            symbol: 'ETH',
            chainId: UniverseChainId.Mainnet,
            // @ts-ignore
            project: {
              logoUrl: 'eth-logo.png',
              safetyLevel: SafetyLevel.Verified,
            },
          }),
        ).toEqual(ethSearchResult)

        expect(
          searchTokenToTokenSearchResult({
            decimals: 18,
            name: 'Ethereum',
            chain: Chain.Ethereum,
            // This is not a mistake, sometimes the standard for ETH is ERC20
            // in search results.
            standard: TokenStandard.Erc20,
            address: NATIVE_CHAIN_ID,
            symbol: 'ETH',
            chainId: UniverseChainId.Mainnet,
            // @ts-ignore
            project: {
              logoUrl: 'eth-logo.png',
              safetyLevel: SafetyLevel.Verified,
            },
          }),
        ).toEqual(ethSearchResult)

        expect(
          searchTokenToTokenSearchResult({
            decimals: 18,
            name: 'Polygon',
            chain: Chain.Polygon,
            standard: TokenStandard.Erc20,
            address: NATIVE_CHAIN_ID,
            symbol: 'MATIC',
            chainId: UniverseChainId.Polygon,
            // @ts-ignore
            project: {
              logoUrl: 'matic-logo.png',
              safetyLevel: SafetyLevel.Verified,
            },
          }),
        ).toEqual({
          type: SearchResultType.Token,
          chainId: UniverseChainId.Polygon,
          address: getNativeAddress(UniverseChainId.Polygon),
          logoUrl: 'matic-logo.png',
          symbol: 'MATIC',
          name: 'Polygon',
          safetyLevel: SafetyLevel.Verified,
        })
      })
    })
    describe(`${TokenStandard.Erc20}`, () => {
      it('accepts a searchToken and returns a TokenSearchResult', () => {
        const tokenSearchResult = {
          type: SearchResultType.Token,
          chainId: 1,
          address: '0x123',
          logoUrl: 'token-logo.png',
          symbol: 'ABC',
          name: 'ABC Token',
          safetyLevel: SafetyLevel.Verified,
        }

        expect(
          searchTokenToTokenSearchResult({
            decimals: 18,
            name: 'ABC Token',
            chain: Chain.Ethereum,
            standard: TokenStandard.Erc20,
            address: '0x123',
            symbol: 'ABC',
            chainId: 1,
            // @ts-ignore
            project: {
              logoUrl: 'token-logo.png',
              safetyLevel: SafetyLevel.Verified,
            },
          }),
        ).toEqual(tokenSearchResult)
      })
    })
  })
})
