import { memo, useMemo } from 'react'; import { __ } from '@wordpress/i18n'; import { Button, Card, CardBody, Flex, FlexBlock, FlexItem, Tooltip, } from '@wordpress/components'; import { edit } from '@wordpress/icons'; import type { Order, OrderItemDimensions } from '../../types/order'; import type { Service } from '../../types/quote'; import type { Price } from '../../types/price'; import type { PluginSettings, } from '../../types/settings'; import CourierBadge from '../../shared/components/courier-badge'; import { formatDisplayNumber } from '../../shared/utils'; interface ServiceGroup { service: Service; orders: Order[]; count: number; } interface ValidationResult { success: boolean; price?: Price; } const ServiceGroupSection = memo(function ServiceGroupSection({ serviceGroups, }: { serviceGroups: ServiceGroup[]; }) { return (

{__('Orders by Service', 'parcel2go-shipping')}

{serviceGroups?.map((group) => ( {`${group.count} ${group.count === 1 ? __('order', 'parcel2go-shipping') : __('orders', 'parcel2go-shipping')}`} ))}
); }); const PriceBreakdownSection = memo(function PriceBreakdownSection({ price, }: { price: Price; }) { const fmt = (amount: number) => `£${(amount / 100).toFixed(2)}`; return (

{__('Price Breakdown', 'parcel2go-shipping')}

{__('Subtotal (ex VAT)', 'parcel2go-shipping')} {fmt(price.net)} {__('VAT', 'parcel2go-shipping')} {fmt(price.tax)}
{__('Total (inc VAT)', 'parcel2go-shipping')} {fmt(price.gross)}
); }); export default function BulkShipBreakdown({ validationResult, orders, defaultPackage, settings, onNavigateToSettings, }: { validationResult: ValidationResult; orders: Order[]; defaultPackage: OrderItemDimensions; settings: PluginSettings; /** Called when the user clicks the edit-settings button. */ onNavigateToSettings?: () => void; }) { const serviceGroups = useMemo(() => { // Group orders by service const groupMap = new Map(); orders.forEach((order) => { if (order.cheapestQuote) { const serviceSlug = order.cheapestQuote.service.slug; if (groupMap.has(serviceSlug)) { const existing = groupMap.get(serviceSlug)!; existing.orders.push(order); existing.count += 1; } else { groupMap.set(serviceSlug, { service: order.cheapestQuote.service, orders: [order], count: 1, }); } } }); // Convert to array and sort by count (descending) then by service name return Array.from(groupMap.values()).sort((a, b) => { if (b.count !== a.count) { return b.count - a.count; } return a.service.name.localeCompare(b.service.name); }); }, [orders]) as ServiceGroup[]; const ordersWithoutService = orders?.filter((order) => !order.cheapestQuote) ?? []; const origin = settings?.shipping; const dimensionUnit = orders?.[0]?.units?.dimension || settings?.units?.dimension || 'cm'; const dimensions = useMemo(() => { let max_length = 0; let max_width = 0; let max_height = 0; let max_weight = 0; orders[0].items?.forEach((item) => { max_length = Math.max(max_length, item.dimensions.length); max_width = Math.max(max_width, item.dimensions.width); max_height = Math.max(max_height, item.dimensions.height); max_weight = Math.max(max_weight, item.dimensions.weight); }); return { length: max_length || defaultPackage?.length, width: max_width || defaultPackage?.width, height: max_height || defaultPackage?.height, weight: max_weight || defaultPackage?.weight, }; }, [orders, defaultPackage]); return ( {/* Order details */}

{__('Order Details', 'parcel2go-shipping')}