import { useDepositService, usePromotion, useWithdrawError, } from '#lib/composables' import type { BaseNadalResponse } from '#lib/types/api' import { usePaymentStore } from '#lib/stores/payment' /** * Import types from Nadal. */ import type { UpdateProfileRequestPayload } from '@kira-dancer/nadal' import type { PaymentWithdrawCryptoRequestPayload } from '#lib/types/withdraw' import { useWithdrawService } from '#lib/composables/service/use-withdraw-service' import { ResponseStatus } from '#lib/enums/app' import { valueToNumber, formatNumberWithCommas, roundNumber } from '#lib/utils' import { useAccount } from '#lib/composables/account' import { reactive } from 'vue' import type { CryptoItem } from '#lib/types' /** * Custom composable function for handling crypto withdrawals. * * @returns An object containing the following functions and data: * - `onCreateWithdrawCrypto`: Function for creating a crypto withdrawal. * - `convertAmountToUSDT`: Function for converting the deposit amount to USDT. * - `updateWalletAddress`: Function for updating the wallet address. * - `cryptoData`: Object containing crypto response data. * @namespace */ export function useWithdrawCrypto() { const { createWithdrawCrypto } = useWithdrawService() const { fetchCrypto } = useDepositService() const { onUpdateProfile } = useAccount() const { isUsingPromotion } = usePromotion() const { openWithdrawErrorModal } = useWithdrawError() const store = usePaymentStore() const isCryptoMaintenance = computed(() => { if (!store.cryptos.length) { return true } if (typeof store.cryptos[0]?.allowWithdraw === 'boolean') { return !store.cryptos[0].allowWithdraw } return false }) const cryptoData = reactive({ value: [] as CryptoItem[], }) /** * Requests crypto data. */ const fetchData = async (): Promise => { const crypto = await fetchCrypto() if (crypto) { cryptoData.value = [crypto] } else { cryptoData.value = [] } } /** * Updates the user's wallet address. * * PaymentWithdrawBankRequestPayload type is imported from the Nadal module. * @param payload - The payload containing the updated profile information. * @returns A promise that resolves when the profile is updated. */ const updateWalletAddress = async ( payload: UpdateProfileRequestPayload, ): Promise => { return await onUpdateProfile(payload) } /** * Creates a crypto withdrawal. * @param payload - The payload containing the withdrawal information. * @returns A promise that resolves with the withdrawal response or void if a promotion is being used. * @throws An error if the withdrawal creation fails. */ const onCreateWithdrawCrypto = async ( payload: PaymentWithdrawCryptoRequestPayload, ): Promise => { if (isUsingPromotion.value) { openWithdrawErrorModal() return Promise.resolve(undefined) } payload.amount = valueToNumber(payload.amount, 1000) const { data, error } = await createWithdrawCrypto(payload) if (error?.value) { if ( error.value?.statusMessage === ResponseStatus.CRYPTO_EXCHANGE_RATE_CHANGED_ERROR ) { await fetchData() } throw error.value } if (data.value?.status === ResponseStatus.OK) { return Promise.resolve(data.value) } } /** * Converts the deposit amount to USDT. * @param depositAmount - The deposit amount to convert. * @param sellPrice - The sell price of the crypto. * @returns The converted amount in USDT. */ const convertAmountToUSDT = ( depositAmount: string, sellPrice: number, ): string => { const amount = valueToNumber(depositAmount, 1000) / sellPrice const roundAmount = roundNumber(amount, 2) return formatNumberWithCommas(roundAmount) } return { onCreateWithdrawCrypto, convertAmountToUSDT, updateWalletAddress, fetchData, cryptoData, isCryptoMaintenance, } }