import { g as getTaskAccessor } from './data-accessor-CVbz8X_7.js';
import { t as taskDeps, s as showTask, b as taskTree } from './tasks-CnwUAUXf.js';
import { json } from '@sveltejs/kit';
import './paths-2M_UxD_H.js';
import 'node:fs';
import 'node:path';
import 'node:os';
import 'node:events';
import 'node:diagnostics_channel';
import 'fs';
import 'events';
import 'util';
import 'path';
import 'assert';
import 'worker_threads';
import 'module';
import 'node:url';
import 'url';
import 'buffer';
import 'node:module';
import 'node:child_process';
import 'node:crypto';
import 'node:process';
import 'node:async_hooks';
import './conduit-Ca_OFWR_.js';
import './indexes-aYlULWG3.js';
import './utils-BvHPJOVk.js';
import './unique-constraint-BM-Apx1H.js';
import './chunk-DDP5xfzh.js';
import './sqlite-Dsbe0wec.js';
import './dual-scope-db-ATrwq0LH.js';
import './memory-schema-Bi7cChwo.js';
import './agent-registry-BtE5uhMO.js';
import './nexus-sqlite2-DgTuv9BQ.js';
import './nexus-schema2-BEd-ol6e.js';
import './logger-ChCwlV5k.js';
import 'node:sqlite';
import './lock-BoHF25mx.js';
import './index-IVoIz4D5.js';
import 'constants';
import 'stream';
import 'node:fs/promises';
import 'jsonc-parser';
import '@cleocode/cant';
import 'node:util';
import 'node:buffer';
import 'child_process';
import './branch-lock-qP1EkZ7p.js';
import './src2-DK8DKufK.js';
import 'llmtxt/identity';
import './sessions-j73dkbCI.js';
import './registry2-BSyCTA3q.js';
import './index-Ca4C2M45.js';
import './mvi-helpers-BNsJ8ZpQ.js';
import './session-store-OwQWMSf_.js';
import './session-manifest-ZHrj54LR.js';
import './storage-B9UA3BHR.js';
import './config-B0nEjgSh.js';
import './json-DXl7iDYg.js';
import './data-safety-central-vmfu_Y4J.js';
import './sqlite-data-accessor-C59wZ0zp.js';
import './cross-db-cleanup-C_FLyd-6.js';

//#region src/routes/api/tasks/graph/+server.ts
/**
* GET /api/tasks/graph?epic=T### — nodes + edges for all deps within an epic.
*
* Returns a sigma-ready graph payload:
*   { nodes: NodeEntry[], edges: EdgeEntry[] }
*
* Nodes include status/priority so the client can color-code them.
* Edges represent task dependency relationships (source depends_on → target).
*
* If `taskId` is provided as a query param, returns only the 1-hop
* neighbourhood (the task itself + direct upstream + direct downstream).
*
* Query params:
*   epic=T###     — all deps within an epic subtree
*   taskId=T###   — 1-hop neighbourhood for a single task
*
* T9617 refactor: zero raw SQL — delegates to core APIs:
*   - 1-hop mode uses `taskDeps` from `@cleocode/core/tasks`
*   - epic mode uses `taskTree` to collect all descendant IDs and their
*     `depends` edges, then filters edges to the epic subtree
*
* @task T9617
*/
/** Recursively collect all FlatTreeNodes into a flat array. */
function collectFlatNodes(nodes) {
	const result = [];
	for (const n of nodes) {
		result.push(n);
		result.push(...collectFlatNodes(n.children));
	}
	return result;
}
var GET = async ({ locals, url }) => {
	const ctx = locals.projectCtx;
	if (!ctx.tasksDbExists) return json({ error: "tasks.db unavailable" }, { status: 503 });
	const epicId = url.searchParams.get("epic");
	const taskId = url.searchParams.get("taskId");
	if (!epicId && !taskId) return json({ error: "epic or taskId query param required" }, { status: 400 });
	try {
		const accessor = await getTaskAccessor(ctx.projectPath);
		if (taskId) {
			const depsResult = await taskDeps(ctx.projectPath, taskId);
			if (!depsResult.success) {
				if (depsResult.error?.code === "E_NOT_FOUND") return json({ error: "task not found" }, { status: 404 });
				return json({ error: depsResult.error?.message ?? "Failed to load deps" }, { status: 500 });
			}
			const focusTask = await accessor.loadSingleTask(taskId);
			if (!focusTask) return json({ error: "task not found" }, { status: 404 });
			const { dependsOn, dependedOnBy } = depsResult.data;
			const nodeMap = /* @__PURE__ */ new Map();
			nodeMap.set(focusTask.id, {
				id: focusTask.id,
				title: focusTask.title,
				status: focusTask.status,
				priority: focusTask.priority,
				type: focusTask.type ?? "task",
				isFocus: true
			});
			for (const t of dependsOn) if (!nodeMap.has(t.id)) nodeMap.set(t.id, {
				id: t.id,
				title: t.title,
				status: t.status,
				priority: "medium",
				type: "task",
				isFocus: false
			});
			for (const t of dependedOnBy) if (!nodeMap.has(t.id)) nodeMap.set(t.id, {
				id: t.id,
				title: t.title,
				status: t.status,
				priority: "medium",
				type: "task",
				isFocus: false
			});
			const edges = [...dependsOn.map((t) => ({
				source: t.id,
				target: taskId
			})), ...dependedOnBy.map((t) => ({
				source: taskId,
				target: t.id
			}))];
			return json({
				nodes: Array.from(nodeMap.values()),
				edges
			});
		}
		if (epicId) {
			if (!await showTask(epicId, ctx.projectPath, accessor).catch((err) => {
				if (err?.code === 4) return null;
				throw err;
			})) return json({
				nodes: [],
				edges: []
			});
			const treeResult = await taskTree(ctx.projectPath, epicId);
			if (!treeResult.success) return json({
				nodes: [],
				edges: []
			});
			const { tree } = treeResult.data;
			const rootNode = tree[0];
			if (!rootNode) return json({
				nodes: [],
				edges: []
			});
			const allDescendants = collectFlatNodes(rootNode.children);
			const allNodes = [{
				id: rootNode.id,
				title: rootNode.title,
				status: rootNode.status,
				type: rootNode.type ?? "epic",
				priority: rootNode.priority,
				depends: rootNode.depends,
				children: rootNode.children
			}, ...allDescendants];
			const idSet = new Set(allNodes.map((n) => n.id));
			const nodes = allNodes.map((n) => ({
				id: n.id,
				title: n.title,
				status: n.status,
				priority: n.priority,
				type: n.type ?? "task",
				isFocus: false
			}));
			const edges = [];
			for (const n of allNodes) for (const depId of n.depends) if (idSet.has(depId)) edges.push({
				source: depId,
				target: n.id
			});
			return json({
				nodes,
				edges
			});
		}
		return json({
			nodes: [],
			edges: []
		});
	} catch (err) {
		return json({ error: String(err) }, { status: 500 });
	}
};

export { GET };
//# sourceMappingURL=_server.ts-Bo4BBJce.js.map
