import { ReactNode, useEffect, useMemo, useRef, useState } from 'react';
import { __ } from '@wordpress/i18n';
import {
Button,
Card,
CardBody,
Flex,
FlexItem,
Icon,
} from '@wordpress/components';
import { check } from '@wordpress/icons';
import { Spinner } from '@woocommerce/components';
import { Quote } from '../../types/quote';
import { useWindowSize } from '../../shared/hooks';
import { Order } from '../../types/order';
import ordersToCheckoutValidatePayload from './utils/ordersToCheckoutValidatePayload';
import { PluginSettings } from 'types/settings';
import { useBulkQuotes } from './hooks/useBulkQuotes';
import { formatCurrency } from '../../shared/utils';
export default function BulkShipOverview({
orders,
onShipNow,
defaultQuotes,
settings,
}: {
orders: Order[];
onShipNow: () => void;
defaultQuotes: Quote[];
settings: PluginSettings;
}) {
const {fetchBulkQuotes, loading: quotesLoading, error, quotes} = useBulkQuotes();
const [isVisible, setIsVisible] = useState(false);
const size = useWindowSize();
const ordersPayload = ordersToCheckoutValidatePayload({ orders, settings });
const Quotes = useMemo(() => quotes ?? defaultQuotes, [quotes, defaultQuotes]);
const quoteRequests = useMemo(
() =>
ordersPayload.items.map((item) => ({
ref: item.ref,
origin: {
country: item.origin.useAddress.country,
postcode: item.origin.useAddress.postcode,
},
destination: {
country: item.destination.useAddress.country,
postcode: item.destination.useAddress.postcode,
},
serviceSlugs: item.serviceSlug ? [item.serviceSlug] : undefined,
parcels: item.parcels.map((x) => ({
ref: x.ref,
weight: x.weight,
height: x.height,
length: x.length,
width: x.width,
value: x.value,
})),
})),
// eslint-disable-next-line react-hooks/exhaustive-deps
[orders, settings]
);
useEffect(() => {
fetchBulkQuotes(quoteRequests);
}, [quoteRequests, fetchBulkQuotes]);
useEffect(() => {
// Trigger animation on mount
const timer = setTimeout(() => setIsVisible(true), 100);
return () => clearTimeout(timer);
}, []);
const totalValue = useMemo(() => {
const shipping =
Quotes?.reduce((acc, quote) => acc + quote.price.gross, 0) ??
0;
const protection =
Quotes?.reduce(
(acc, quote) =>
acc +
(quote.availableExtras.find(
(extra) => extra.key === 'protection:base'
)?.price.gross ?? 0),
0
) ?? 0;
return (shipping + protection) / 100;
}, [Quotes]);
const { displayPrice, isAnimating } = useAnimatedPrice(
quotesLoading ? 0 : totalValue,
800,
quotesLoading
);
const orderLabel =
orders.length === 1
? __('1 order', 'parcel2go-shipping')
: `${orders.length} ${__('orders', 'parcel2go-shipping')}`;
const BulkShipContent = useMemo(
(): ReactNode => (
{__('Ship', 'parcel2go-shipping')} {orderLabel}
{__('Ship', 'parcel2go-shipping')} {orderLabel}{' '}
{__('for', 'parcel2go-shipping')}
{displayPrice === 0 && quotesLoading ? (