import type { CashbackInformation, PromotionUsageDetail } from '#lib/types' import { useCancelPromotion, usePromotion } from '#lib/composables/promotion' import { formatAmountUnit } from '#lib/utils' import { computed } from 'vue' import { useRuntimeConfig } from 'nuxt/app' /** * Composable to manage cashback promotion details and usage information. * * Provides computed properties for displaying cashback information and * promotion usage details, as well as methods for interacting with promotions. * * @returns {Object} An object containing: * - `cashbackInformation`: Computed cashback details based on promotion type. * - `promotionUsageDetail`: Computed details about promotion usage. * - `isUsingPromotion`: Reactive reference indicating if a promotion is in use. * - `isCommissionType`: Reactive reference indicating if the promotion type is commission. * - `fetchUserPromotion`: Function to get user promotion details. * - `fetchPromotionPackages`: Function to get available promotion packages. * - `openModalCancelPromotion`: Function to open the cancel promotion modal. * @namespace */ export function useCashbackPromotion() { // Destructure methods and reactive properties from usePromotion composable. const { promotionUser, packageId, isUsingPromotion, isCommissionType, fetchUserPromotion, fetchPromotionPackages, } = usePromotion() // Access the function to open the cancel promotion modal. const { openModalCancelPromotion } = useCancelPromotion() const MAIN_CREDIT_UNIT = useRuntimeConfig().public.MAIN_CREDIT_UNIT /** * @private * Computed property for generating cashback information based on the promotion type. * - For commission type promotions, displays today's total bet, yesterday's refund, and total cashback. * - For non-commission type promotions with a valid package ID, displays deposit amount and promotion amount. * @returns {CashbackInformation[]} An array of cashback information objects. */ const cashbackInformation = computed( (): CashbackInformation[] => { if (isCommissionType.value) { return [ { title: 'Tổng cược hôm nay', // Today's total bet type: 'totalToday', value: `${formatAmountUnit(promotionUser.value.total_bet!, 1)} ${MAIN_CREDIT_UNIT}`, }, { title: 'Hoàn trả hôm qua', // Yesterday's refund type: 'totalYesterday', value: `${formatAmountUnit(promotionUser.value.yesterday_refund!, 1)} ${MAIN_CREDIT_UNIT}`, }, { title: 'Tổng hoàn trả', // Total cashback type: 'totalCashback', value: `${formatAmountUnit(promotionUser.value.total_refund!, 1)} ${MAIN_CREDIT_UNIT}`, }, ] } const isPackageWithoutCommission = packageId.value && !isCommissionType.value if (isPackageWithoutCommission) { return [ { title: 'Số tiền nạp', // Deposit amount type: 'depositAmount', value: `${formatAmountUnit(promotionUser.value.deposit_amount!, 1)} ${MAIN_CREDIT_UNIT}`, }, { title: 'Số tiền khuyến mãi', // Promotion amount imgSrc: 'promotionAmount', value: `${formatAmountUnit(promotionUser.value.promotion_amount!, 1)} ${MAIN_CREDIT_UNIT}`, }, ] } return [] }, ) /** * Computed property for generating promotion usage details. * * Calculates the total number of rounds, current round, and the ratio of completed rounds. * * @returns {PromotionUsageDetail} Object containing total rounds, current round, and round ratio. */ const promotionUsageDetail = computed( (): PromotionUsageDetail => { const { rolling, multiplier } = promotionUser.value const turnOver = promotionUser.value.turnover || 0 const dividedNumber = rolling && multiplier ? rolling / multiplier : 0 const currentRound = dividedNumber && turnOver ? Math.floor(turnOver / dividedNumber) : 0 return { totalRound: multiplier ?? 0, currentRound, ratioRound: (currentRound / (multiplier ?? 1)) * 100, } }, ) return { cashbackInformation, promotionUsageDetail, isUsingPromotion, isCommissionType, fetchUserPromotion, fetchPromotionPackages, openModalCancelPromotion, } }