import { n as useMutation, p as useQueryClient, u as useQuery } from "../_libs/react+tanstack__react-query.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/cart-query-hooks-Bz8ID9jY.js
function getCartKey(cartType) {
	return `local-cart-${cartType}`;
}
function readCart(cartType) {
	try {
		const raw = localStorage.getItem(getCartKey(cartType));
		return raw ? JSON.parse(raw) : [];
	} catch {
		return [];
	}
}
function writeCart(cartType, items) {
	localStorage.setItem(getCartKey(cartType), JSON.stringify(items));
}
function useGetCart(cartType = "regular") {
	return useQuery({
		queryKey: [getCartKey(cartType)],
		queryFn: () => {
			const items = readCart(cartType);
			return {
				items,
				totalItems: items.reduce((sum, item) => sum + item.quantity, 0),
				totalAmount: 0
			};
		}
	});
}
function useAddToCart(cartType = "regular") {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: async ({ productId, quantity, storeId, slotId, deliveryDate }) => {
			const items = readCart(cartType);
			const existing = items.find((i) => i.productId === productId);
			if (existing) {
				existing.quantity += quantity;
				if (slotId) existing.slotId = slotId;
				if (deliveryDate) existing.deliveryDate = deliveryDate;
			} else items.push({
				id: Date.now(),
				productId,
				quantity,
				storeId,
				addedAt: Date.now(),
				slotId: slotId ?? null,
				deliveryDate: deliveryDate ?? null
			});
			writeCart(cartType, items);
		},
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: [getCartKey(cartType)] });
		}
	});
}
function useUpdateCartItem(cartType = "regular") {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: async ({ productId, quantity, slotId, deliveryDate }) => {
			const items = readCart(cartType);
			const existing = items.find((i) => i.productId === productId);
			if (existing) {
				existing.quantity = quantity;
				if (slotId !== void 0) existing.slotId = slotId;
				if (deliveryDate !== void 0) existing.deliveryDate = deliveryDate;
			}
			writeCart(cartType, items);
		},
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: [getCartKey(cartType)] });
		}
	});
}
function useRemoveFromCart(cartType = "regular") {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: async (productId) => {
			let items = readCart(cartType);
			items = items.filter((i) => i.productId !== productId);
			writeCart(cartType, items);
		},
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: [getCartKey(cartType)] });
		}
	});
}
function clearLocalCart(cartType = "regular") {
	localStorage.removeItem(getCartKey(cartType));
}
//#endregion
export { useUpdateCartItem as a, useRemoveFromCart as i, useAddToCart as n, useGetCart as r, clearLocalCart as t };
