import { t as useGraphMetadata } from "./use-graph-metadata-C0FHs2qM.js";
import { useMemo } from "react";
//#region editors/knowledge-vault/hooks/use-knowledge-mocs.ts
const TIER_RANK = {
	HUB: 0,
	DOMAIN: 1,
	TOPIC: 2
};
const UNTIERED_RANK = 99;
const MOC_NOTE_TYPE_RE = /^MOC \((HUB|DOMAIN|TOPIC)\)$/;
/**
* Sidebar/GraphView source for MoC documents.
*
* Reuses the in-flight `useGraphMetadata()` response — the graph-indexer
* already projects MoCs into `graph_nodes` (note_type prefixed `MOC (...)`)
* and their coreIdeas/childRefs into `graph_edges` (link_type `CORE_IDEA`).
* No additional fetch is performed here.
*
* Note on duplication: `useKnowledgeNotes` and this hook each call
* `useGraphMetadata()` independently — React does not dedupe hook calls.
* Both run from the single `DriveExplorer` mount, so two concurrent fetches
* fire on drive open. The long-term fix is to lift `useGraphMetadata()` into
* a shared context/provider so all consumers share one fetch instance.
*
* `coreIdea.contextPhrase` is not in the projection — only the target
* document id. Returned as empty string; GraphView uses it for tooltips
* only.
*/
function useKnowledgeMocs() {
	const { nodeMap, edges } = useGraphMetadata();
	return useMemo(() => {
		const mocIds = /* @__PURE__ */ new Set();
		for (const node of nodeMap.values()) if (node.noteType?.startsWith("MOC (")) mocIds.add(node.documentId);
		const edgesBySource = /* @__PURE__ */ new Map();
		for (const edge of edges) {
			let arr = edgesBySource.get(edge.sourceDocumentId);
			if (!arr) {
				arr = [];
				edgesBySource.set(edge.sourceDocumentId, arr);
			}
			arr.push(edge);
		}
		const mocs = [];
		for (const node of nodeMap.values()) {
			if (!node.noteType || !node.noteType.startsWith("MOC (")) continue;
			const tier = MOC_NOTE_TYPE_RE.exec(node.noteType)?.[1] ?? null;
			const coreIdeas = [];
			const childRefs = [];
			const sourceEdges = edgesBySource.get(node.documentId) ?? [];
			for (const edge of sourceEdges) if (edge.linkType === "CHILD_MOC") childRefs.push(edge.targetDocumentId);
			else if (edge.linkType === "CORE_IDEA") if (mocIds.has(edge.targetDocumentId)) childRefs.push(edge.targetDocumentId);
			else coreIdeas.push({
				noteRef: edge.targetDocumentId,
				contextPhrase: ""
			});
			mocs.push({
				id: node.documentId,
				title: node.title ?? "(untitled)",
				tier,
				noteCount: coreIdeas.length,
				coreIdeas,
				childRefs
			});
		}
		mocs.sort((a, b) => {
			const ra = a.tier ? TIER_RANK[a.tier] : UNTIERED_RANK;
			const rb = b.tier ? TIER_RANK[b.tier] : UNTIERED_RANK;
			if (ra !== rb) return ra - rb;
			return a.title.localeCompare(b.title);
		});
		const mocMap = /* @__PURE__ */ new Map();
		for (const moc of mocs) mocMap.set(moc.id, moc);
		return {
			mocs,
			mocMap
		};
	}, [nodeMap, edges]);
}
//#endregion
export { useKnowledgeMocs as t };

//# sourceMappingURL=use-knowledge-mocs-CxCi7giP.js.map