import React, { useMemo } from 'react' import { Box, Flex, Text } from 'theme-ui' import { FormattedMessage, useIntl } from 'react-intl' import type { PropsWithChildren } from 'react' import type { Item } from '../../types' import { Items } from './Items' export interface Props { items: T[] variant: string removeItem: (item: T) => Promise | void updateItem: (item: T, oldItem?: T) => PromiseLike | unknown numberFormat: (num: number) => string } const useCartSections = (items: T[]) => useMemo( () => items.reduce( (acc, curr) => { if (curr.availability === 'available') { if (curr.sellingPrice === 0) { acc.gifts.push(curr) } else { acc.products.push(curr) } } else { acc.unavailable.push(curr) } return acc }, { gifts: [] as T[], products: [] as T[], unavailable: [] as T[] } ), [items] ) const MinicartDrawerContent = ({ items, variant: v, removeItem, updateItem, numberFormat, }: PropsWithChildren>) => { const { formatMessage } = useIntl() const { gifts, products, unavailable } = useCartSections(items) const variant = `${v}.content` return ( {unavailable.length > 0 && ( {formatMessage({ id: 'minicart.warning.unavailable' })} )} {unavailable.length > 0 && products.length > 0 && ( {formatMessage( { id: 'minicart.section.available' }, { count: products.length } )} )} {products.length > 0 && ( )} {gifts.length > 0 && ( <> )} ) } export default MinicartDrawerContent