import { t as supabase } from "./client-_1GYITl-.mjs";
import { t as useQuery } from "../_libs/tanstack__react-query.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/data-hooks--e_NaUze.js
function useItems() {
	return useQuery({
		queryKey: ["items"],
		queryFn: async () => {
			const { data, error } = await supabase.from("items_master").select("*").order("sort_order");
			if (error) throw error;
			return data ?? [];
		}
	});
}
function useUploads() {
	return useQuery({
		queryKey: ["uploads"],
		queryFn: async () => {
			const { data, error } = await supabase.from("uploads").select("*").order("year", { ascending: false }).order("month", { ascending: false });
			if (error) throw error;
			return data ?? [];
		}
	});
}
/** Latest upload period per division; returns array of {division, month, year}. */
function useLatestPeriods() {
	const { data: uploads, ...rest } = useUploads();
	const latest = /* @__PURE__ */ new Map();
	(uploads ?? []).forEach((u) => {
		if (!latest.has(u.division)) latest.set(u.division, {
			division: u.division,
			month: u.month,
			year: u.year
		});
	});
	return {
		data: Array.from(latest.values()),
		...rest
	};
}
/** All fact rows for the latest available period per division (AEN-level by default). */
function useLatestFacts(level = "AEN") {
	const { data: periods } = useLatestPeriods();
	return useQuery({
		queryKey: [
			"facts-latest",
			level,
			periods.map((p) => `${p.division}-${p.year}-${p.month}`).join("|")
		],
		enabled: periods.length > 0,
		queryFn: async () => {
			const rows = [];
			for (const p of periods) {
				const { data, error } = await supabase.from("fact_progress").select("*").eq("division", p.division).eq("month", p.month).eq("year", p.year).eq("level", level);
				if (error) throw error;
				rows.push(...data ?? []);
			}
			return rows;
		}
	});
}
function useFactsForUpload(uploadId) {
	return useQuery({
		queryKey: ["facts", uploadId],
		enabled: !!uploadId,
		queryFn: async () => {
			const { data, error } = await supabase.from("fact_progress").select("*").eq("upload_id", uploadId).order("aen").order("sse", { nullsFirst: true }).order("item_sr_no");
			if (error) throw error;
			return data ?? [];
		}
	});
}
//#endregion
export { useUploads as i, useItems as n, useLatestFacts as r, useFactsForUpload as t };
