import { DEFAULT_NETWORK , getWalletConfig , STATIC_CONTACTS } from '@config' ; import { Contact , ExtendedContact , ExtendedContract , IAccount , Network , NetworkId , TAddress , WalletId } from '@types' ; import { isSameAddress } from '@utils' ; export const getLabelByAccount = ( account , addressLabels ) : ExtendedContact | undefined => { if ( ! account || ! addressLabels ) return ; return addressLabels . find ( ( label ) => isSameAddress ( account . address , label . address as TAddress ) && account . networkId === label . network ) ; } ; export const getLabelByAddressAndNetwork = ( address , addressLabels , network ) : ExtendedContact | undefined => { if ( ! address ) return ; return addressLabels . find ( ( label ) => isSameAddress ( address as TAddress , label . address as TAddress ) && ( network ? network . id === label . network : true ) ) ; } ; const getUnusedLabel = ( contacts , generateLabel ) => { let index = 0 ; let isFound ; let unusedLabel ; do { index += 1 ; unusedLabel = generateLabel ( index ) ; isFound = contacts . find ( ( a ) => a . label === unusedLabel ) ; } while ( isFound ) ; return unusedLabel ; } ; export const findMultipleNextUnusedDefaultLabels = ( wallet , numOfLabels ) => ( contacts ) : string [ ] => { let tempContacts = contacts ; const newLabels = [ ] ; for ( let z = 0 ; z < numOfLabels ; z ++ ) { const newLabel = getUnusedLabel ( tempContacts , ( index ) => `${ getWalletConfig ( wallet ) . name } Account ${ index }` ) ; newLabels . push ( newLabel ) ; tempContacts = [ ... tempContacts , { address : '' , notes : '' , network : DEFAULT_NETWORK , label : newLabel } ] ; } return newLabels ; } ; export const findNextRecipientLabel = ( contacts ) => getUnusedLabel ( contacts , ( index ) => `Recipient ${ index }` ) ; export const getContactFromContracts = ( contracts ) => ( address , networkId ? ) : ExtendedContact | undefined => { const contract = contracts . find ( ( c ) => c . address === address && ( c . networkId === networkId || ! networkId ) ) ; const contact = contract && { address , label : contract . name , network : contract . networkId , notes : '' , uuid : contract . uuid } ; return contact ; } ; export const getContactByAddressAndNetworkId = ( contacts , contracts ) => ( address , networkId ) => { return ( [ ... contacts , ... STATIC_CONTACTS ] . filter ( ( contact ) => contact . network === networkId ) . find ( ( contact ) => isSameAddress ( contact . address as TAddress , address ) ) || getContactFromContracts ( contracts ) ( address , networkId ) ) ; } ;