import "./dist-CYoGQ6PD.mjs";
import "./document-drive-BKF7N19I.mjs";
import "./chunk-VBDAOXYI-Dd3pepio.mjs";
import { c as useDocumentsInSelectedDrive, h as useSelectedDrive, i as setSelectedNode, t as addDocument, v as useSetPHAppConfig } from "./dist-LFSG_0Q3.mjs";
import { t as DocumentBrowser } from "./DocumentBrowser-BXa-p16u.mjs";
import "./load-design-tokens-B8bpWdLf.mjs";
import { useCallback, useMemo, useState } from "react";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
//#region editors/applicant-workspace/components/ApplicantDashboard.tsx
const DOC_TYPE = {
	project: "rfp-hub/project",
	grantApplication: "rfp-hub/grant-application"
};
const REVIEW_STAGE_CLASS = {
	DRAFT: "rfp-status-draft",
	SUBMITTED: "rfp-status-pending",
	OPENED: "rfp-status-pending",
	UNDER_REVIEW: "rfp-status-review",
	NEEDS_REVISION: "rfp-status-pending",
	REVISED: "rfp-status-review",
	APPROVED: "rfp-status-approved",
	CONDITIONALLY_APPROVED: "rfp-status-approved",
	REJECTED: "rfp-status-rejected",
	WITHDRAWN: "rfp-status-closed",
	FUNDED: "rfp-status-funded",
	COMPLETED: "rfp-status-completed"
};
const GROUPS = [
	{
		key: "drafts",
		label: "Drafts",
		description: "Not submitted yet",
		stages: ["DRAFT", "NEEDS_REVISION"]
	},
	{
		key: "inFlight",
		label: "In flight",
		description: "Awaiting funder review",
		stages: [
			"SUBMITTED",
			"OPENED",
			"UNDER_REVIEW",
			"REVISED"
		]
	},
	{
		key: "decided",
		label: "Decided",
		description: "Outcome received",
		stages: [
			"APPROVED",
			"CONDITIONALLY_APPROVED",
			"REJECTED",
			"WITHDRAWN"
		]
	},
	{
		key: "funded",
		label: "Funded / Active",
		description: "Money received or work completed",
		stages: ["FUNDED", "COMPLETED"]
	}
];
function isDocumentOfType(doc, type) {
	return doc.header.documentType === type;
}
function ApplicantDashboard({ children }) {
	const showDocumentEditor = !!children;
	const documents = useDocumentsInSelectedDrive();
	const [selectedDrive] = useSelectedDrive();
	const [creatingType, setCreatingType] = useState(null);
	const driveId = selectedDrive?.header.id;
	const projects = useMemo(() => (documents ?? []).filter((d) => isDocumentOfType(d, DOC_TYPE.project)), [documents]);
	const applications = useMemo(() => (documents ?? []).filter((d) => isDocumentOfType(d, DOC_TYPE.grantApplication)), [documents]);
	const hasProject = projects.length > 0;
	const handleCreate = useCallback(async (documentType, name) => {
		if (!driveId || creatingType) return;
		setCreatingType(documentType);
		try {
			const node = await addDocument(driveId, name, documentType);
			if (node?.id) setSelectedNode(node.id);
		} catch (err) {
			console.error(`Failed to create ${documentType}:`, err);
		} finally {
			setCreatingType(null);
		}
	}, [driveId, creatingType]);
	const applicationsByGroup = useMemo(() => {
		const grouped = {
			drafts: [],
			inFlight: [],
			decided: [],
			funded: []
		};
		for (const app of applications) {
			const stage = app.state.global.reviewStage;
			const group = GROUPS.find((g) => g.stages.includes(stage));
			if (group) grouped[group.key].push(app);
		}
		return grouped;
	}, [applications]);
	const needsRevision = useMemo(() => applications.filter((a) => a.state.global.reviewStage === "NEEDS_REVISION"), [applications]);
	return /* @__PURE__ */ jsxs("div", {
		style: {
			display: "flex",
			height: "100%"
		},
		children: [/* @__PURE__ */ jsx(DocumentBrowser, {
			mode: "applicant",
			onCreate: handleCreate,
			creatingType,
			canCreate: !!driveId
		}), /* @__PURE__ */ jsx("div", {
			style: {
				flex: 1,
				overflowY: "auto"
			},
			children: showDocumentEditor ? children : /* @__PURE__ */ jsx("div", {
				className: "rfp-page",
				children: /* @__PURE__ */ jsx("div", {
					className: "rfp-page-inner",
					children: !hasProject ? /* @__PURE__ */ jsx(OnboardingHero, {
						isCreating: creatingType === DOC_TYPE.project,
						onCreate: () => handleCreate(DOC_TYPE.project, "My project")
					}) : /* @__PURE__ */ jsxs(Fragment, { children: [
						/* @__PURE__ */ jsxs("header", {
							className: "rfp-header",
							children: [
								/* @__PURE__ */ jsx("span", {
									className: "rfp-meta",
									children: "RFP Hub · My workspace"
								}),
								/* @__PURE__ */ jsx("h1", {
									className: "rfp-h1",
									children: "Your applications"
								}),
								/* @__PURE__ */ jsx("p", {
									className: "rfp-hint",
									children: "Keep your project profile fresh and track every application you've submitted."
								})
							]
						}),
						needsRevision.length > 0 ? /* @__PURE__ */ jsx(ReviewerFeedbackSpotlight, { apps: needsRevision }) : null,
						/* @__PURE__ */ jsxs("div", {
							className: "rfp-grid-4",
							children: [
								/* @__PURE__ */ jsx(StatTile, {
									label: "Drafts",
									value: applicationsByGroup.drafts.length
								}),
								/* @__PURE__ */ jsx(StatTile, {
									label: "In flight",
									value: applicationsByGroup.inFlight.length
								}),
								/* @__PURE__ */ jsx(StatTile, {
									label: "Decided",
									value: applicationsByGroup.decided.length
								}),
								/* @__PURE__ */ jsx(StatTile, {
									label: "Funded",
									value: applicationsByGroup.funded.length
								})
							]
						}),
						/* @__PURE__ */ jsxs("section", {
							className: "rfp-card rfp-section",
							children: [
								/* @__PURE__ */ jsxs("div", {
									className: "rfp-row",
									style: { justifyContent: "space-between" },
									children: [/* @__PURE__ */ jsxs("h2", {
										className: "rfp-section-subtitle",
										children: ["Project profile · ", projects.length]
									}), /* @__PURE__ */ jsx("button", {
										type: "button",
										className: "rfp-btn-secondary",
										disabled: creatingType === DOC_TYPE.project || !driveId,
										onClick: () => handleCreate(DOC_TYPE.project, `Project ${projects.length + 1}`),
										children: creatingType === DOC_TYPE.project ? "Creating…" : "+ Add another project"
									})]
								}),
								/* @__PURE__ */ jsx("hr", { className: "rfp-divider" }),
								/* @__PURE__ */ jsx("ul", {
									className: "rfp-list",
									children: projects.map((p) => /* @__PURE__ */ jsx(ProjectListItem, {
										project: p,
										onClick: () => setSelectedNode(p.header.id)
									}, p.header.id))
								})
							]
						}),
						/* @__PURE__ */ jsxs("section", {
							className: "rfp-card rfp-section",
							children: [
								/* @__PURE__ */ jsxs("div", {
									className: "rfp-row",
									style: { justifyContent: "space-between" },
									children: [/* @__PURE__ */ jsxs("h2", {
										className: "rfp-section-subtitle",
										children: ["Applications · ", applications.length]
									}), /* @__PURE__ */ jsx("span", {
										className: "rfp-meta",
										children: "DAOIP-5 GrantApplication"
									})]
								}),
								/* @__PURE__ */ jsx("hr", { className: "rfp-divider" }),
								applications.length === 0 ? /* @__PURE__ */ jsx(EmptyApplicationsCallout, {}) : /* @__PURE__ */ jsx("div", {
									className: "rfp-col",
									style: { gap: 24 },
									children: GROUPS.map((group) => {
										const apps = applicationsByGroup[group.key];
										if (apps.length === 0) return null;
										return /* @__PURE__ */ jsxs("div", {
											className: "rfp-col",
											style: { gap: 12 },
											children: [/* @__PURE__ */ jsx("div", {
												className: "rfp-row",
												style: { justifyContent: "space-between" },
												children: /* @__PURE__ */ jsxs("div", {
													className: "rfp-col",
													style: { gap: 2 },
													children: [/* @__PURE__ */ jsxs("h3", {
														className: "rfp-stage-column-title",
														children: [
															group.label,
															" · ",
															apps.length
														]
													}), /* @__PURE__ */ jsx("span", {
														className: "rfp-locked-hint",
														children: group.description
													})]
												})
											}), /* @__PURE__ */ jsx("div", {
												className: "rfp-grid-2",
												children: apps.map((app) => /* @__PURE__ */ jsx(ApplicationCard, {
													app,
													onClick: () => setSelectedNode(app.header.id)
												}, app.header.id))
											})]
										}, group.key);
									})
								})
							]
						}),
						/* @__PURE__ */ jsx(FusionDiscoveryCallout, {})
					] })
				})
			})
		})]
	});
}
function OnboardingHero({ isCreating, onCreate }) {
	return /* @__PURE__ */ jsx("div", {
		style: { padding: "48px 0" },
		children: /* @__PURE__ */ jsxs("div", {
			className: "rfp-hero",
			children: [
				/* @__PURE__ */ jsx("span", {
					className: "rfp-hero-badge",
					children: "Step 1 of 2"
				}),
				/* @__PURE__ */ jsx("h2", {
					className: "rfp-hero-title",
					children: "Complete your profile"
				}),
				/* @__PURE__ */ jsx("p", {
					className: "rfp-hero-body",
					children: "Welcome to your Applicant Workspace. Start by creating your project profile — it becomes your reusable \"common application\" across every pool you apply to. No profile, no applications."
				}),
				/* @__PURE__ */ jsx("button", {
					type: "button",
					className: "rfp-btn-primary-lg",
					onClick: onCreate,
					disabled: isCreating,
					children: isCreating ? "Creating…" : "Create your project profile →"
				})
			]
		})
	});
}
function StatTile({ label, value }) {
	return /* @__PURE__ */ jsxs("div", {
		className: "rfp-stat-tile",
		children: [/* @__PURE__ */ jsx("span", {
			className: "rfp-stat-tile-label",
			children: label
		}), /* @__PURE__ */ jsx("span", {
			className: "rfp-stat-tile-value",
			children: value
		})]
	});
}
function ProjectListItem({ project, onClick }) {
	const state = project.state.global;
	return /* @__PURE__ */ jsxs("li", {
		className: "rfp-list-item",
		onClick,
		role: "button",
		tabIndex: 0,
		style: { cursor: "pointer" },
		onKeyDown: (e) => {
			if (e.key === "Enter" || e.key === " ") onClick();
		},
		children: [/* @__PURE__ */ jsxs("div", {
			className: "rfp-col",
			style: {
				gap: 4,
				flex: 1
			},
			children: [/* @__PURE__ */ jsx("strong", { children: state.name ?? project.header.name ?? "Unnamed project" }), state.description ? /* @__PURE__ */ jsx("span", {
				className: "rfp-meta",
				style: {
					textTransform: "none",
					letterSpacing: "normal",
					maxWidth: 520,
					display: "-webkit-box",
					WebkitLineClamp: 2,
					WebkitBoxOrient: "vertical",
					overflow: "hidden"
				},
				children: state.description
			}) : /* @__PURE__ */ jsx("span", {
				className: "rfp-meta",
				children: "rfp-hub/project"
			})]
		}), /* @__PURE__ */ jsxs("span", {
			className: "rfp-chip",
			children: [state.relevantTo?.length ?? 0, " pools"]
		})]
	});
}
function ApplicationCard({ app, onClick }) {
	const state = app.state.global;
	const stageClass = REVIEW_STAGE_CLASS[state.reviewStage] ?? "rfp-status-draft";
	return /* @__PURE__ */ jsxs("div", {
		className: "rfp-doc-card",
		onClick,
		role: "button",
		tabIndex: 0,
		onKeyDown: (e) => {
			if (e.key === "Enter" || e.key === " ") onClick();
		},
		children: [
			/* @__PURE__ */ jsx("span", {
				className: "rfp-doc-card-title",
				children: state.grantPoolName ?? "Unlinked pool"
			}),
			/* @__PURE__ */ jsx("span", {
				className: "rfp-doc-card-meta",
				children: state.projectName ?? app.header.name ?? "Unnamed application"
			}),
			/* @__PURE__ */ jsxs("div", {
				className: "rfp-row",
				style: {
					justifyContent: "space-between",
					gap: 6
				},
				children: [/* @__PURE__ */ jsx("span", {
					className: `rfp-status-badge ${stageClass}`,
					children: state.reviewStage.replace(/_/g, " ").toLowerCase()
				}), state.revisionCount > 0 ? /* @__PURE__ */ jsxs("span", {
					className: "rfp-doc-card-meta",
					children: ["rev ", state.revisionCount]
				}) : null]
			})
		]
	});
}
function ReviewerFeedbackSpotlight({ apps }) {
	return /* @__PURE__ */ jsxs("section", {
		className: "rfp-feedback-card",
		children: [/* @__PURE__ */ jsxs("h3", {
			className: "rfp-feedback-card-title",
			children: ["Needs revision · ", apps.length]
		}), apps.slice(0, 3).map((app) => /* @__PURE__ */ jsxs("div", {
			className: "rfp-col",
			style: { gap: 4 },
			children: [
				/* @__PURE__ */ jsxs("strong", {
					className: "rfp-body",
					style: { fontWeight: 600 },
					children: [
						app.state.global.projectName ?? app.header.name,
						" →",
						" ",
						app.state.global.grantPoolName ?? "pool"
					]
				}),
				/* @__PURE__ */ jsx("p", {
					className: "rfp-feedback-card-body",
					children: app.state.global.feedbackNotes ?? "Reviewer requested changes. Open the application to see details."
				}),
				/* @__PURE__ */ jsx("button", {
					type: "button",
					className: "rfp-btn-secondary",
					style: { alignSelf: "flex-start" },
					onClick: () => setSelectedNode(app.header.id),
					children: "Address feedback →"
				})
			]
		}, app.header.id))]
	});
}
function FusionDiscoveryCallout() {
	return /* @__PURE__ */ jsxs("section", {
		className: "rfp-fusion-card",
		children: [
			/* @__PURE__ */ jsx("span", {
				className: "rfp-meta",
				children: "Discover"
			}),
			/* @__PURE__ */ jsx("h3", {
				className: "rfp-h2",
				style: { fontSize: 20 },
				children: "Looking for new pools?"
			}),
			/* @__PURE__ */ jsxs("p", {
				className: "rfp-body",
				style: { margin: 0 },
				children: [
					"Browse open calls on Fusion — the public discovery frontend. Click",
					" ",
					/* @__PURE__ */ jsx("strong", { children: "Apply" }),
					" on any pool that fits your project; a new grant application will be provisioned in this workspace, pre-filled from your project profile."
				]
			}),
			/* @__PURE__ */ jsx("button", {
				type: "button",
				className: "rfp-fusion-link",
				onClick: () => {
					if (typeof window !== "undefined") window.open("https://rfp-hub.vetra.io/", "_blank");
				},
				children: "Browse pools on Fusion →"
			})
		]
	});
}
function EmptyApplicationsCallout() {
	return /* @__PURE__ */ jsx("div", {
		className: "rfp-empty",
		style: {
			padding: 24,
			textAlign: "left"
		},
		children: /* @__PURE__ */ jsxs("div", {
			className: "rfp-col",
			style: { gap: 8 },
			children: [/* @__PURE__ */ jsx("strong", {
				style: { color: "var(--rfp-on-surface)" },
				children: "No applications yet."
			}), /* @__PURE__ */ jsxs("span", { children: [
				"Applications are created by applying to a pool from Fusion. Open Fusion, find a pool that fits your project, and click",
				" ",
				/* @__PURE__ */ jsx("strong", { children: "Apply" }),
				" — a new",
				" ",
				/* @__PURE__ */ jsx("code", { children: "rfp-hub/grant-application" }),
				" document will be provisioned here, pre-filled from your project profile."
			] })]
		})
	});
}
//#endregion
//#region editors/applicant-workspace/config.ts
/** Editor config for the <%= pascalCaseAppName %> */
const editorConfig = {
	isDragAndDropEnabled: true,
	allowedDocumentTypes: ["rfp-hub/project", "rfp-hub/grant-application"]
};
//#endregion
//#region editors/applicant-workspace/editor.tsx
function Editor(props) {
	useSetPHAppConfig(editorConfig);
	return /* @__PURE__ */ jsx("div", {
		className: "rfp-scope",
		style: { height: "100%" },
		children: /* @__PURE__ */ jsx(ApplicantDashboard, { ...props })
	});
}
//#endregion
export { Editor as default };

//# sourceMappingURL=editor-BIGbou8t.mjs.map