import { g as getTaskAccessor } from './data-accessor-CVbz8X_7.js';
import { 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/tree/[epicId]/+server.ts
/**
* GET /api/tasks/tree/[epicId] — full epic hierarchy (epic → tasks → subtasks).
*
* Returns nested tree up to 3 levels deep.
*
* T9617 refactor: zero raw SQL — delegates to `taskTree` + `showTask` from
* `@cleocode/core/tasks`. The `epic` field preserves the pre-T9617 shape
* with a `children` array built from the FlatTreeNode tree returned by core.
*
* @task T9617
*/
/**
* Convert a core FlatTreeNode into the legacy TreeNode shape.
* Core FlatTreeNode does not carry all the fields (verification_json,
* acceptance_json, etc.) so we accept them as `null` for now.
* The Studio tree view only uses id/title/status/priority for rendering.
*/
function flatNodeToTreeNode(node, depth) {
	return {
		id: node.id,
		title: node.title,
		status: node.status,
		priority: node.priority,
		type: node.type ?? "task",
		pipeline_stage: null,
		size: null,
		verification_json: null,
		acceptance_json: null,
		created_at: (/* @__PURE__ */ new Date()).toISOString(),
		completed_at: null,
		children: depth < 3 ? node.children.map((c) => flatNodeToTreeNode(c, depth + 1)) : []
	};
}
var GET = async ({ locals, params }) => {
	const ctx = locals.projectCtx;
	if (!ctx.tasksDbExists) return json({ error: "tasks.db unavailable" }, { status: 503 });
	const { epicId } = params;
	try {
		const accessor = await getTaskAccessor(ctx.projectPath);
		let epicDetail;
		try {
			epicDetail = await showTask(epicId, ctx.projectPath, accessor);
		} catch (err) {
			if (err?.code === 4) return json({ error: "not found" }, { status: 404 });
			throw err;
		}
		const treeResult = await taskTree(ctx.projectPath, epicId);
		if (!treeResult.success) return json({ error: treeResult.error?.message ?? "Failed to build tree" }, { status: 500 });
		const { tree } = treeResult.data;
		const rootNode = tree[0];
		const children = rootNode?.children.map((c) => flatNodeToTreeNode(c, 1)) ?? [];
		const allFlatNodes = [epicDetail];
		function collectAll(nodes) {
			for (const n of nodes) {
				allFlatNodes.push(n);
				collectAll(n.children);
			}
		}
		if (rootNode) collectAll(rootNode.children);
		const stats = {
			total: allFlatNodes.length,
			done: allFlatNodes.filter((t) => t.status === "done").length,
			active: allFlatNodes.filter((t) => t.status === "active").length,
			pending: allFlatNodes.filter((t) => t.status === "pending").length,
			archived: allFlatNodes.filter((t) => t.status === "archived").length
		};
		return json({
			epic: {
				id: epicDetail.id,
				title: epicDetail.title,
				status: epicDetail.status,
				priority: epicDetail.priority,
				type: epicDetail.type ?? "epic",
				pipeline_stage: epicDetail.pipelineStage ?? null,
				size: epicDetail.size ?? null,
				verification_json: epicDetail.verification !== void 0 && epicDetail.verification !== null ? JSON.stringify(epicDetail.verification) : null,
				acceptance_json: epicDetail.acceptance && epicDetail.acceptance.length > 0 ? JSON.stringify(epicDetail.acceptance) : null,
				created_at: epicDetail.createdAt,
				completed_at: epicDetail.completedAt ?? null,
				children
			},
			stats
		});
	} catch (err) {
		return json({ error: String(err) }, { status: 500 });
	}
};

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