/* eslint-disable @typescript-eslint/no-explicit-any */
import _identity from 'lodash/identity'
import _isEmpty from 'lodash/isEmpty'
import _map from 'lodash/map'
import React, { memo, useState } from 'react'
import { FEES_STYLES } from '../../constants'
import { createFixedFloatNormalizer, currencyNormalizerCreator } from '../../normalizers'
import { IOrderData } from '../../types'
import { CONFIGS } from '../../utils'
import Countdown from 'react-countdown'
import { showZero } from '../../utils/showZero'
import { IRenderer } from '../timerWidget'
interface OrderDetailsProps {
orderData: any;
paymentFieldsData: any[];
customMobileText?: string;
handleCountdownFinish?: () => void;
}
interface CountdownI {
expiresAt: number;
handleCountdownFinish: () => void;
}
const SimpleCountdown = memo(({ expiresAt, handleCountdownFinish }: CountdownI) => {
const renderer = ({
minutes,
seconds,
completed,
handleCountdownFinish,
}: IRenderer) => {
if (completed) {
handleCountdownFinish()
return null
}
return (
{showZero(minutes)}:{showZero(seconds)}
)
}
return (
{expiresAt && (
renderer({
...props,
handleCountdownFinish,
})
}
/>
)}
)
})
export const OrderDetails = ({
orderData = {},
paymentFieldsData = [],
customMobileText = 'Your order total',
handleCountdownFinish = _identity
}: OrderDetailsProps) => {
const { currency, guest_count } = orderData || {}
const hasTableTypes = Boolean(Number(guest_count))
const [isExpanded, setIsExpanded] = useState(false)
// Find the total field to display in the mobile view
const totalField = paymentFieldsData.find(field => field.id === 'total')
const totalValue =
totalField && orderData.total
? totalField.normalizer
? totalField.normalizer(orderData.total, currency, orderData)
: orderData.total
: ''
const toggleExpand = () => {
setIsExpanded(!isExpanded)
}
const defaultItemRenderer = (item: any) => {
return (
{item.quantity}
{' x '}
{item.groupName ? item.groupName + ' - ' : ''}
{item.name}
{' - '}
{CONFIGS.FEES_STYLE === FEES_STYLES.TRADITIONAL &&
currencyNormalizerCreator(
createFixedFloatNormalizer(2)(parseFloat(item.price)),
currency,
) + ' (incl. fees)'}
{CONFIGS.FEES_STYLE === FEES_STYLES.DISPLAY_BOTH &&
currencyNormalizerCreator(
createFixedFloatNormalizer(2)(parseFloat(item.cost)),
currency,
)}
{CONFIGS.FEES_STYLE === FEES_STYLES.FINAL_WITH_BREAKDOWN &&
currencyNormalizerCreator(
createFixedFloatNormalizer(2)(parseFloat(item.price)),
currency,
)}
{' each'}
{CONFIGS.FEES_STYLE === FEES_STYLES.DISPLAY_BOTH && (
{`(${currencyNormalizerCreator(
createFixedFloatNormalizer(2)(
parseFloat(String(item.price)),
),
currency,
)} with fees)`}
)}
{CONFIGS.FEES_STYLE === FEES_STYLES.FINAL_WITH_BREAKDOWN && parseFloat(item.price) - parseFloat(item.cost) > 0 && (
{`(${currencyNormalizerCreator(createFixedFloatNormalizer(2)(parseFloat(item.cost)), currency)} + ${currencyNormalizerCreator(createFixedFloatNormalizer(2)(parseFloat(item.price) - parseFloat(item.cost)), currency)} fee)`}
)}
)
}
const defaultTableRenderer = (item: any) => {
return (
{item.groupName ? item.groupName + ' - ' : ''}
{item.name}
{' - '}
Guest Count{': '}
{item.guestCount}
{CONFIGS.FEES_STYLE === FEES_STYLES.TRADITIONAL &&
currencyNormalizerCreator(
createFixedFloatNormalizer(2)(parseFloat(item.price)),
currency,
) + ' (incl. fees)'}
{CONFIGS.FEES_STYLE === FEES_STYLES.DISPLAY_BOTH &&
currencyNormalizerCreator(
createFixedFloatNormalizer(2)(parseFloat(item.cost)),
currency,
)}
{CONFIGS.FEES_STYLE === FEES_STYLES.FINAL_WITH_BREAKDOWN &&
currencyNormalizerCreator(
createFixedFloatNormalizer(2)(parseFloat(item.price)),
currency,
)}
{CONFIGS.FEES_STYLE === FEES_STYLES.DISPLAY_BOTH && (
{`(${currencyNormalizerCreator(
createFixedFloatNormalizer(2)(
parseFloat(String(item.price)),
),
currency,
)} with fees)`}
)}
{CONFIGS.FEES_STYLE === FEES_STYLES.FINAL_WITH_BREAKDOWN && parseFloat(item.price) - parseFloat(item.cost) > 0 && (
{`(${currencyNormalizerCreator(createFixedFloatNormalizer(2)(parseFloat(item.cost)), currency)} + ${currencyNormalizerCreator(createFixedFloatNormalizer(2)(parseFloat(item.price) - parseFloat(item.cost)), currency)} fee)`}
)}
)
}
return (
{/* Mobile view summary */}
{!isExpanded && (
{totalValue}
)}
{orderData?.expires_at && (
)}
{/* Original content - will be hidden on mobile when collapsed */}
{_map(paymentFieldsData, field => {
const { id, label, className = '', normalizer = _identity } = field
const value = orderData[id as keyof IOrderData] || ''
let component = null
if (field.id === 'add_ons' && _isEmpty(value)) {
return false
}
if (field.id === 'tableTypes') {
const valueArray = value as Array
component = (
{_map(valueArray, tableTypeItem => (
Table Type
{tableTypeItem.ticketType}
Number of Tables
{tableTypeItem.count}
Guest Count
{tableTypeItem.quantity}
))}
)
}
return (
component || (
{label}
{typeof value === 'string' || typeof value === 'number'
? normalizer(value, currency, orderData)
: _map(value, item => (
item.isTable ? defaultTableRenderer(item) : defaultItemRenderer(item)
))}
)
)
})}
)
}