import { IHttpClient } from '@/common/http'; import { IJoliCoin, IGem } from '@/common/rewards/type'; import { IUnlockOptionsEvent } from '@/common/rewards/reward-emitter'; import { fetchBalance as fetchJoliCoinBalance } from '../jolicoin/fetch-balance'; import { fetchGemBalance } from '../joligem/fetch-gem-balance'; export type CurrencyType = 'JOLI_COIN' | 'JOLI_GEM'; export interface CurrencyHandler { fetchBalance: (httpClient: IHttpClient) => Promise<{ balance: number } | undefined>; getQuantity: (unlockOptions: IUnlockOptionsEvent | null) => number; getCurrentUser: (unlockOptions: IUnlockOptionsEvent | null) => IJoliCoin | IGem; createUpdatedUser: (balance: number, enableAutoDeduct: boolean) => IJoliCoin | IGem; getUpdateKey: () => keyof Pick; isEnough: (unlockOptions: IUnlockOptionsEvent | null, user: IJoliCoin | IGem) => boolean; } export const CURRENCY_HANDLERS: Record = { JOLI_COIN: { fetchBalance: fetchJoliCoinBalance, getQuantity: (unlockOptions) => unlockOptions?.options?.find((option) => option.type === 'JOLI_COIN')?.joliCoinChoices[0] .joliCoinQuantity ?? 0, getCurrentUser: (unlockOptions) => unlockOptions?.userJoliCoin ?? { balance: 0, enableAutoDeduct: false }, createUpdatedUser: (balance, enableAutoDeduct) => ({ balance, enableAutoDeduct }), getUpdateKey: () => 'userJoliCoin', isEnough: (unlockOptions, user) => unlockOptions?.options?.some( (option) => option.type === 'JOLI_COIN' && option.joliCoinChoices.some((choice) => choice.joliCoinQuantity <= (user?.balance ?? 0)) ) ?? false }, JOLI_GEM: { fetchBalance: fetchGemBalance, getQuantity: (unlockOptions) => unlockOptions?.options?.find((option) => option.type === 'JOLI_GEM')?.joliGemChoice?.joliGemQuantity ?? 0, getCurrentUser: (unlockOptions) => unlockOptions?.userGem ?? { balance: 0, enableAutoDeduct: false }, createUpdatedUser: (balance, enableAutoDeduct) => ({ balance, enableAutoDeduct }), getUpdateKey: () => 'userGem', isEnough: (unlockOptions, user) => { const joliGemConfig = unlockOptions?.options.find((option) => option.type === 'JOLI_GEM'); if (joliGemConfig) { return (joliGemConfig.joliGemChoice?.joliGemQuantity ?? 0) <= (user?.balance ?? 0); } return false; } } };