import { AccountAssetDTO , getAccountBalanceControllerGetQueryKey , RequestWithdrawalDTO , useTransferControllerRequestWithdrawal , } from '@energyweb/exchange-react-query-client' ; import { showNotification , NotificationTypeEnum , } from '@energyweb/origin-ui-core' ; import { PowerFormatter } from '@energyweb/origin-ui-utils' ; import { Dispatch , SetStateAction } from 'react' ; import { useTranslation } from 'react-i18next' ; import { useQueryClient } from 'react-query' ; export const useWithdrawCertificateHandler = ( address , exchangeCertificates , resetList , setTxPending ) => { const { t } = useTranslation ( ) ; const { mutate } = useTransferControllerRequestWithdrawal ( ) ; const queryClient = useQueryClient ( ) ; const exchangeCertificatesQueryKey = getAccountBalanceControllerGetQueryKey ( ) ; return < Id > ( id , amount ) => { setTxPending ( true ) ; const assetId = exchangeCertificates . find ( ( cert ) => cert . asset . id === ( id as unknown as AccountAssetDTO [ 'asset' ] [ 'id' ] ) ) ?. asset . id ; const data = { assetId , address , amount : PowerFormatter . getBaseValueFromValueInDisplayUnit ( Number ( amount ) ) . toString ( ) , } ; mutate ( { data } , { onSuccess : ( ) => { showNotification ( t ( 'certificate.exchangeInbox.notifications.withdrawSuccess' ) , NotificationTypeEnum . Success ) ; queryClient . invalidateQueries ( exchangeCertificatesQueryKey ) ; resetList ( ) ; } , onError : ( error ) => { showNotification ( `${ t ( 'certificate.exchangeInbox.notifications.withdrawError' ) }:
            ${ error ?. response ?. data ?. message || '' }
            ` , NotificationTypeEnum . Error ) ; } , onSettled : ( ) => setTxPending ( false ) , } ) ; } ; } ;