import { n as __toESM } from "../_runtime.mjs";
import { I as require_jsx_runtime } from "../_libs/@radix-ui/react-alert-dialog+[...].mjs";
import { t as PageHeader } from "./page-header-BE1EG5aP.mjs";
import { u as require_react } from "../_libs/@floating-ui/react-dom+[...].mjs";
import { i as CardTitle, n as CardContent, r as CardHeader, t as Card } from "./card-BXjpJ96D.mjs";
import { n as useItems, r as useLatestFacts } from "./data-hooks--e_NaUze.mjs";
import { o as zoneClass, r as compliancePct } from "./calculations-JyW9rCxF.mjs";
import { i as TabsTrigger, n as TabsContent, r as TabsList, t as Tabs } from "./tabs-CCJRliUM.mjs";
//#region node_modules/.nitro/vite/services/ssr/assets/heat-maps-4mSGqr4U.js
var import_react = /* @__PURE__ */ __toESM(require_react());
var import_jsx_runtime = require_jsx_runtime();
function Cell({ label, value, sub }) {
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
		className: `rounded-md p-3 text-center ${zoneClass(value)}`,
		children: [
			/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
				className: "text-xs opacity-80 truncate",
				title: label,
				children: label
			}),
			/* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
				className: "text-lg font-semibold mt-1",
				children: [value.toFixed(0), "%"]
			}),
			sub && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
				className: "text-[10px] opacity-70 mt-0.5",
				children: sub
			})
		]
	});
}
function HeatMaps() {
	const { data: aenFacts = [] } = useLatestFacts("AEN");
	const { data: sseFacts = [] } = useLatestFacts("SSE");
	const { data: items = [] } = useItems();
	const divMap = (0, import_react.useMemo)(() => {
		const m = /* @__PURE__ */ new Map();
		for (const r of aenFacts) {
			if (!m.has(r.division)) m.set(r.division, {
				s: 0,
				p: 0
			});
			const g = m.get(r.division);
			g.s += r.cum_scope;
			g.p += r.cum_progress;
		}
		return Array.from(m.entries()).map(([k, v]) => ({
			k,
			c: compliancePct(v.p, v.s)
		}));
	}, [aenFacts]);
	const aenMap = (0, import_react.useMemo)(() => {
		const m = /* @__PURE__ */ new Map();
		for (const r of aenFacts) {
			const key = `${r.division}|${r.aen}`;
			if (!m.has(key)) m.set(key, {
				div: r.division,
				aen: r.aen,
				s: 0,
				p: 0
			});
			const g = m.get(key);
			g.s += r.cum_scope;
			g.p += r.cum_progress;
		}
		return Array.from(m.values()).map((v) => ({
			...v,
			c: compliancePct(v.p, v.s)
		}));
	}, [aenFacts]);
	const sseMap = (0, import_react.useMemo)(() => {
		const m = /* @__PURE__ */ new Map();
		for (const r of sseFacts) {
			if (!r.sse) continue;
			const key = `${r.division}|${r.aen}|${r.sse}`;
			if (!m.has(key)) m.set(key, {
				div: r.division,
				aen: r.aen,
				sse: r.sse,
				s: 0,
				p: 0
			});
			const g = m.get(key);
			g.s += r.cum_scope;
			g.p += r.cum_progress;
		}
		return Array.from(m.values()).map((v) => ({
			...v,
			c: compliancePct(v.p, v.s)
		}));
	}, [sseFacts]);
	const itemMap = (0, import_react.useMemo)(() => {
		const nameMap = new Map(items.map((i) => [i.sr_no, i.name]));
		const m = /* @__PURE__ */ new Map();
		for (const r of aenFacts) {
			if (!m.has(r.item_sr_no)) m.set(r.item_sr_no, {
				sr: r.item_sr_no,
				s: 0,
				p: 0
			});
			const g = m.get(r.item_sr_no);
			g.s += r.cum_scope;
			g.p += r.cum_progress;
		}
		return Array.from(m.values()).map((v) => ({
			...v,
			c: compliancePct(v.p, v.s),
			name: nameMap.get(v.sr) ?? v.sr
		})).sort((a, b) => a.c - b.c);
	}, [aenFacts, items]);
	return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", {
		className: "p-6 max-w-[1600px] mx-auto",
		children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(PageHeader, {
			title: "Heat Maps",
			subtitle: "Compliance colours — green ≥ 90%, yellow 70–90%, orange 50–70%, red < 50%."
		}), /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Tabs, {
			defaultValue: "division",
			children: [
				/* @__PURE__ */ (0, import_jsx_runtime.jsxs)(TabsList, { children: [
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TabsTrigger, {
						value: "division",
						children: "Divisions"
					}),
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TabsTrigger, {
						value: "aen",
						children: "AENs"
					}),
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TabsTrigger, {
						value: "sse",
						children: "SSEs"
					}),
					/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TabsTrigger, {
						value: "item",
						children: "Items"
					})
				] }),
				/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TabsContent, {
					value: "division",
					children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardTitle, { children: "Division Heat Map" }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardContent, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
						className: "grid grid-cols-2 md:grid-cols-4 gap-3",
						children: divMap.map((d) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Cell, {
							label: d.k,
							value: d.c
						}, d.k))
					}) })] })
				}),
				/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TabsContent, {
					value: "aen",
					children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardTitle, { children: "AEN Heat Map" }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardContent, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
						className: "grid grid-cols-2 md:grid-cols-4 lg:grid-cols-6 gap-3",
						children: aenMap.map((d) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Cell, {
							label: d.aen,
							value: d.c,
							sub: d.div
						}, `${d.div}-${d.aen}`))
					}) })] })
				}),
				/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TabsContent, {
					value: "sse",
					children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardTitle, { children: "SSE Heat Map" }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardContent, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
						className: "grid grid-cols-2 md:grid-cols-4 lg:grid-cols-6 gap-3",
						children: sseMap.map((d) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Cell, {
							label: d.sse,
							value: d.c,
							sub: `${d.div} · ${d.aen}`
						}, `${d.div}-${d.aen}-${d.sse}`))
					}) })] })
				}),
				/* @__PURE__ */ (0, import_jsx_runtime.jsx)(TabsContent, {
					value: "item",
					children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(Card, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardHeader, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardTitle, { children: "Item Heat Map (lowest compliance first)" }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CardContent, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", {
						className: "grid grid-cols-2 md:grid-cols-4 lg:grid-cols-6 gap-3",
						children: itemMap.slice(0, 60).map((d) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Cell, {
							label: d.name,
							value: d.c,
							sub: d.sr
						}, d.sr))
					}) })] })
				})
			]
		})]
	});
}
//#endregion
export { HeatMaps as component };
