import { useResiduesContext } from "../contexts/ResiduesContext.tsx";
import { notifications } from "../../../shared/lib/notifications.ts";

const useResiduesPdf = () => {
    const { palletIdsToPrint, boxIdsToPrint } = useResiduesContext();

    const basePdfUrl = `${import.meta.env.VITE_API_URL}/residues/pdf`;

    const getPdf = (url: string) => {
        const pdfWindow = window.open(url);
        if (!pdfWindow) return;
        pdfWindow.print();
    };

    const onGetPalletsPdfClick = () => {
        if (palletIdsToPrint.size === 0 && boxIdsToPrint.size === 0) {
            notifications.show({ message: "Не выбран ни один элемент для печати "});
            return;
        }

        const palletIdsStr = palletIdsToPrint.values().toArray().join(",")
        const boxIdsStr = boxIdsToPrint.values().toArray().join(",")
        getPdf(`${basePdfUrl}/?pallet_ids=${palletIdsStr}&box_ids=${boxIdsStr}`);
    };

    return {
        onGetPalletsPdfClick,
    };
};

export default useResiduesPdf;
