import { CURRENCY_HANDLERS, CurrencyType } from '../../coins/commands/currency-handlers'; import { rewardsCommands } from '../../coins/rewards-command'; import { unlockOptionsHandler } from '../../common'; import { createEventPromiseHandler } from '../../event-listener'; import { IUseSubscriptionResultEvent, UseSubscriptionResultEventName } from '@/common/rewards/reward-emitter'; import { rewardsEmitter, InvokeSubscriptionEventName } from '@/common/rewards/reward-emitter'; export const registerUseSubscriptionCommand = (params: { showSubscriptionModal: () => Promise<'SUCCESS' | 'CANCEL' | 'FAILED'>; }) => { const { showSubscriptionModal } = params; rewardsCommands.registerCommand(`Rewards.subscription`, async () => { const unlockOptions = await unlockOptionsHandler.getData(); const type = unlockOptions?.options?.find((option) => option.type === 'JOLI_GEM')?.type; // Use currency handler for balance and quantity calculation const handler = CURRENCY_HANDLERS[type ? 'JOLI_GEM' : 'JOLI_COIN']; const currentUser = handler.getCurrentUser(unlockOptions); // if enough, pass this command into JOLI_GEM or JOLI_COIN command if (handler.isEnough(unlockOptions, currentUser)) { return { result: 'FAILED' }; } const subscriptionResult = await showSubscriptionModal(); return { result: subscriptionResult === 'SUCCESS' ? 'SUCCESS' : 'FAILED' }; }); }; // EVENTS export const createShowSubscriptionModal = (type: 'SUBSCRIPTION') => { return async () => { const modalHandler = createEventPromiseHandler< IUseSubscriptionResultEvent, typeof UseSubscriptionResultEventName >(rewardsEmitter, UseSubscriptionResultEventName); rewardsEmitter.emit(InvokeSubscriptionEventName, type); const modalResult = await modalHandler.getFreshData(); return modalResult.result; }; };