import { _ as useSelectedNode, c as useDocumentsInSelectedDrive, i as setSelectedNode } from "./dist-LFSG_0Q3.mjs";
import { useMemo, useState } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
//#region editors/funder-back-office/components/DocumentBrowser.tsx
function pickPoolDotClass(state) {
	switch (state.lifecycle) {
		case "OPEN": return "rfp-dot-open";
		case "UPCOMING": return "rfp-dot-upcoming";
		case "CLOSED": return "rfp-dot-closed";
		case "AWARDED": return "rfp-dot-awarded";
		case "CANCELLED": return "rfp-dot-cancelled";
		case "REQUEST_FOR_COMMENTS": return "rfp-dot-rfc";
		case "NOT_AWARDED": return "rfp-dot-rejected";
		default: return "rfp-dot-draft";
	}
}
function pickAppDotClass(stage) {
	switch (stage) {
		case "SUBMITTED":
		case "OPENED":
		case "NEEDS_REVISION": return "rfp-dot-pending";
		case "UNDER_REVIEW":
		case "REVISED": return "rfp-dot-review";
		case "APPROVED":
		case "CONDITIONALLY_APPROVED": return "rfp-dot-approved";
		case "REJECTED": return "rfp-dot-rejected";
		case "WITHDRAWN": return "rfp-dot-closed";
		case "FUNDED": return "rfp-dot-funded";
		case "COMPLETED": return "rfp-dot-completed";
		default: return "rfp-dot-draft";
	}
}
function pickSystemDotClass(verificationState) {
	switch (verificationState) {
		case "VERIFIED": return "rfp-dot-approved";
		case "PENDING_REVIEW": return "rfp-dot-pending";
		case "SUSPENDED": return "rfp-dot-closed";
		case "REVOKED": return "rfp-dot-rejected";
		default: return "rfp-dot-draft";
	}
}
function DocumentBrowser({ mode, onCreate, creatingType, canCreate, extraDocuments }) {
	const driveDocs = useDocumentsInSelectedDrive() ?? [];
	const documents = useMemo(() => {
		if (!extraDocuments?.length) return driveDocs;
		const byId = /* @__PURE__ */ new Map();
		for (const d of driveDocs) byId.set(d.header.id, d);
		for (const d of extraDocuments) byId.set(d.header.id, d);
		return Array.from(byId.values());
	}, [driveDocs, extraDocuments]);
	const selected = useSelectedNode();
	const selectedId = selected?.id;
	const groups = useMemo(() => {
		if (mode === "funder") return [
			{
				key: "systems",
				label: "Grant systems",
				docType: "rfp-hub/grant-system",
				creatable: true,
				createLabel: "+ New grant system",
				itemFor: (doc) => {
					const d = doc;
					return {
						title: d.state.global.name ?? doc.header.name ?? "Unnamed organization",
						dotClass: pickSystemDotClass(d.state.global.verificationState)
					};
				}
			},
			{
				key: "pools",
				label: "Grant pools",
				docType: "rfp-hub/grant-pool",
				creatable: true,
				createLabel: "+ New grant pool",
				itemFor: (doc) => {
					const d = doc;
					return {
						title: d.state.global.name ?? doc.header.name ?? "Untitled pool",
						dotClass: pickPoolDotClass(d.state.global)
					};
				}
			},
			{
				key: "applications",
				label: "Applications",
				docType: "rfp-hub/grant-application",
				creatable: false,
				createLabel: "",
				filter: (doc) => doc.state.global.reviewStage !== "DRAFT",
				itemFor: (doc) => {
					const d = doc;
					return {
						title: d.state.global.projectName ?? doc.header.name ?? "Unnamed application",
						dotClass: pickAppDotClass(d.state.global.reviewStage)
					};
				}
			},
			{
				key: "governance",
				label: "Governance",
				docType: "rfp-hub/governance",
				creatable: true,
				createLabel: "+ New governance",
				itemFor: (doc) => ({
					title: doc.header.name ?? "Governance",
					dotClass: "rfp-dot-draft"
				})
			}
		];
		return [{
			key: "projects",
			label: "Projects",
			docType: "rfp-hub/project",
			creatable: true,
			createLabel: "+ New project",
			itemFor: (doc) => {
				const stateName = doc.state.global.name?.trim();
				const headerName = doc.header.name?.trim();
				return {
					title: stateName || headerName || "Unnamed project",
					dotClass: "rfp-dot-approved"
				};
			}
		}, {
			key: "applications",
			label: "Applications",
			docType: "rfp-hub/grant-application",
			creatable: false,
			createLabel: "",
			itemFor: (doc) => {
				const d = doc;
				return {
					title: d.state.global.projectName ?? doc.header.name ?? "Unnamed application",
					dotClass: pickAppDotClass(d.state.global.reviewStage)
				};
			}
		}];
	}, [mode]);
	const grouped = useMemo(() => {
		const result = {};
		for (const group of groups) result[group.key] = documents.filter((d) => d.header.documentType === group.docType && (group.filter ? group.filter(d) : true));
		return result;
	}, [documents, groups]);
	const [collapsed, setCollapsed] = useState({});
	const toggle = (key) => setCollapsed((prev) => ({
		...prev,
		[key]: !prev[key]
	}));
	const totalDocs = documents.length;
	return /* @__PURE__ */ jsxs("aside", {
		className: "rfp-browser",
		children: [/* @__PURE__ */ jsxs("button", {
			type: "button",
			className: "rfp-browser-header rfp-browser-home-button",
			onClick: () => setSelectedNode(void 0),
			"aria-label": selected ? "Back to dashboard" : "Dashboard home",
			title: selected ? "Back to dashboard" : "You are on the dashboard",
			children: [/* @__PURE__ */ jsxs("div", {
				className: "rfp-browser-home-button-inner",
				children: [/* @__PURE__ */ jsx("h3", {
					className: "rfp-browser-title",
					children: mode === "funder" ? "Back office" : "My workspace"
				}), /* @__PURE__ */ jsxs("span", {
					className: "rfp-hint",
					children: [
						totalDocs,
						" document",
						totalDocs === 1 ? "" : "s"
					]
				})]
			}), selected ? /* @__PURE__ */ jsx("span", {
				className: "rfp-browser-back-hint",
				children: "← Back"
			}) : null]
		}), groups.map((group) => {
			const docs = grouped[group.key];
			const isCollapsed = collapsed[group.key] === true;
			const creating = creatingType === group.docType;
			return /* @__PURE__ */ jsxs("div", {
				className: "rfp-browser-group",
				children: [/* @__PURE__ */ jsxs("button", {
					type: "button",
					className: "rfp-browser-group-header",
					onClick: () => toggle(group.key),
					"aria-expanded": !isCollapsed,
					children: [/* @__PURE__ */ jsxs("span", {
						className: "rfp-browser-group-label",
						children: [
							isCollapsed ? "▸" : "▾",
							" ",
							group.label
						]
					}), /* @__PURE__ */ jsx("span", {
						className: "rfp-browser-group-count",
						children: docs.length
					})]
				}), !isCollapsed && /* @__PURE__ */ jsxs(Fragment, { children: [docs.length === 0 ? /* @__PURE__ */ jsx("div", {
					className: "rfp-browser-empty",
					children: "No documents yet."
				}) : docs.map((doc) => {
					const meta = group.itemFor(doc);
					return /* @__PURE__ */ jsxs("button", {
						type: "button",
						className: `rfp-browser-item${selectedId === doc.header.id ? " rfp-browser-item-active" : ""}`,
						onClick: () => setSelectedNode(doc.header.id),
						title: meta.title,
						children: [/* @__PURE__ */ jsx("span", { className: `rfp-browser-dot ${meta.dotClass}` }), /* @__PURE__ */ jsx("span", {
							className: "rfp-browser-item-name",
							children: meta.title
						})]
					}, doc.header.id);
				}), group.creatable && canCreate ? /* @__PURE__ */ jsx("button", {
					type: "button",
					className: "rfp-browser-create",
					disabled: creating,
					onClick: () => onCreate(group.docType, `${group.label.replace(/s$/, "")} ${docs.length + 1}`),
					children: creating ? "Creating…" : group.createLabel
				}) : null] })]
			}, group.key);
		})]
	});
}
//#endregion
export { DocumentBrowser as t };

//# sourceMappingURL=DocumentBrowser-BXa-p16u.mjs.map