import { useState } from 'react' import { useTranslation } from 'react-i18next' import lengthBlueBtn from '@/assets/system/length-blue-btn.webp' import { CenterModal } from '@/components/center-modal.tsx' import { SmartBackground } from '@/components/smart-background.tsx' import { Input } from '@/components/ui/input.tsx' import { Switch } from '@/components/ui/switch.tsx' import { AUTO_HOSTING_DEFAULT_SINGLE_WIN_THRESHOLD } from '@/constants' import { notify } from '@/lib/notify' import { useModalStore } from '@/store' import { useAuthStore } from '@/store/auth' import { type AutoHostingStopRules, selectSelectionTotal, useGameAutoHostingStore, useGameRoundStore, useGameSessionStore, } from '@/store/game' function parseAmount(value: string) { const parsed = Number(value) return Number.isFinite(parsed) && parsed > 0 ? parsed : 0 } function parseBalance(value: string | number | null | undefined) { if (typeof value === 'number') { return Number.isFinite(value) ? value : 0 } if (typeof value !== 'string') { return 0 } const parsed = Number(value) return Number.isFinite(parsed) ? parsed : 0 } function DesktopAutoSettingModal() { const { t } = useTranslation() const open = useModalStore((state) => state.modals.desktopAutoSetting) const setModalOpen = useModalStore((state) => state.setModalOpen) const currentUser = useAuthStore((state) => state.currentUser) const round = useGameRoundStore((state) => state.round) const selections = useGameRoundStore((state) => state.selections) const totalBetAmount = useGameRoundStore(selectSelectionTotal) const tableLimitMax = useGameSessionStore( (state) => state.dashboard.tableLimitMax, ) const startHosting = useGameAutoHostingStore((state) => state.startHosting) const [balanceLimitEnabled, setBalanceLimitEnabled] = useState(false) const [balanceLimitValue, setBalanceLimitValue] = useState('0') const [singleWinLimitEnabled, setSingleWinLimitEnabled] = useState(false) const [singleWinLimitValue, setSingleWinLimitValue] = useState( String(AUTO_HOSTING_DEFAULT_SINGLE_WIN_THRESHOLD), ) const [jackpotStopEnabled, setJackpotStopEnabled] = useState(false) function handleClose() { setModalOpen('desktopAutoSetting', false) } function handleSubmit() { if (round.phase !== 'betting' || !round.id) { notify.warning(t('commonUi.toast.betUnavailable')) handleClose() return } if (selections.length === 0) { notify.warning(t('commonUi.toast.selectNumbersBeforeAutoHosting')) handleClose() return } const balance = parseBalance(currentUser?.coin) if (tableLimitMax > 0 && totalBetAmount > tableLimitMax) { notify.warning(t('commonUi.toast.betLimitExceeded')) return } if (totalBetAmount > balance) { notify.warning(t('commonUi.toast.insufficientBalance')) return } const rules: AutoHostingStopRules = { stopIfBalanceBelow: { amount: parseAmount(balanceLimitValue), enabled: balanceLimitEnabled, }, stopIfSingleWinAbove: { amount: parseAmount(singleWinLimitValue), enabled: singleWinLimitEnabled, }, stopOnJackpot: jackpotStopEnabled, } startHosting({ balanceAfterBet: balance, rules, selections, }) notify.success(t('commonUi.toast.autoHostingStarted')) handleClose() } return ( {t('game.modals.autoSetting.title')} } isNormalBg={true} titleAlign="left" className={'w-design-835 !h-design-500'} >
{t('game.modals.autoSetting.rows.stopIfBalanceLowerThan')}
setBalanceLimitValue(event.target.value)} className={ 'game-setting-input h-full w-design-280 text-design-18' } />
{t('game.modals.autoSetting.rows.stopIfSingleWinExceeds')}
setSingleWinLimitValue(event.target.value)} className={ 'game-setting-input h-full w-design-280 text-design-18' } />
{t('game.modals.autoSetting.rows.stopOnAnyJackpot')}
{t('game.modals.autoSetting.startAutoSpin')}
) } export default DesktopAutoSettingModal