import React, { useState, useEffect } from 'react'
import { View } from 'react-native'
import { Fade, Placeholder, PlaceholderLine } from 'rn-placeholder';
import { useTheme } from 'styled-components/native'
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import {
PaymentOptionWallet as PaymentOptionWalletController,
useLanguage,
useUtils,
useOrder,
useConfig
} from 'ordering-components/native'
import {
Container,
SectionLeft,
} from './styles'
import { OText } from '../shared'
const PaymentOptionWalletUI = (props: any) => {
const {
businessConfigs,
businessId,
walletsState,
selectWallet,
deletetWalletSelected
} = props
const theme = useTheme()
const [, t] = useLanguage()
const [{ configs }] = useConfig()
const [{ carts }] = useOrder()
const [{ parsePrice }] = useUtils()
const cart = carts?.[`businessId:${businessId}`] ?? {}
const isWalletCashEnabled = configs?.wallet_cash_enabled?.value === '1'
const isWalletPointsEnabled = configs?.wallet_credit_point_enabled?.value === '1'
const isBusinessWalletCashEnabled = businessConfigs.find((config: any) => config.key === 'wallet_cash_enabled')?.value === '1'
const isBusinessWalletPointsEnabled = businessConfigs.find((config: any) => config.key === 'wallet_credit_point_enabled')?.value === '1'
const [checkedState, setCheckedState] = useState(
new Array(walletsState.result?.length).fill(false)
);
const creditBalance: any = (wallet: any) => ` = ${parsePrice(wallet.balance / wallet.redemption_rate, { isTruncable: true })}`
const walletName: any = {
cash: {
name: t('PAY_WITH_CASH_WALLET', 'Pay with Cash Wallet'),
isActive: isWalletCashEnabled && isBusinessWalletCashEnabled
},
credit_point: {
name: t('PAY_WITH_CREDITS_POINTS_WALLET', 'Pay with Credit Points Wallet'),
isActive: isWalletPointsEnabled && isBusinessWalletPointsEnabled
}
}
const handleOnChange = (position: any, wallet: any) => {
const updatedCheckedState = checkedState.map((item: any, index: any) =>
index === position ? !item : item
);
if (!checkedState[position]) {
selectWallet(wallet)
} else {
deletetWalletSelected(wallet)
}
setCheckedState(updatedCheckedState);
};
useEffect(() => {
if (!walletsState.loading && walletsState.result?.length) {
setCheckedState(
walletsState.result?.map((wallet: any) => {
return !!cart?.payment_events?.find((w: any) => w.wallet_id === wallet.id)
})
)
}
}, [walletsState.result?.length])
return (
<>
{!walletsState.loading &&
!walletsState.error &&
walletsState.result?.length > 0 &&
(
<>
{walletsState.result?.map((wallet: any, idx: any) => wallet.valid && wallet.balance >= 0 && walletName[wallet.type]?.isActive && (
wallet.valid)?.length - 1}
onPress={() => handleOnChange(idx, wallet)}
disabled={(cart?.balance === 0 && !checkedState[idx]) || wallet.balance === 0}
>
{checkedState[idx] ? (
) : (
)}
{walletName[wallet.type]?.name}
{wallet.type === 'cash' && (
{parsePrice(wallet?.balance, { isTruncable: true })}
)}
{wallet.type === 'credit_point' && (
{`${wallet?.balance} ${t('POINTS', 'Points')}`}
{wallet?.balance > 0
? creditBalance(wallet)
: null}
)}
))}
>
)}
{walletsState?.loading && (
{[...Array(2).keys()].map(i => (
))}
)}
>
)
}
export const PaymentOptionWallet = (props: any) => {
const paymentWalletProps = {
...props,
UIComponent: PaymentOptionWalletUI
}
return (
)
}