import * as path$1 from "node:path";
import * as fs$1 from "node:fs";

//#region src/agents/subagents/orchestration/orchestration-manifest.ts
/**
* Orchestration manifest lifecycle — create, update, complete, read.
* The manifest.json is the durable record of orchestration state.
*/
/**
* Write `manifest.json` and an auto-generated `README.md` at the orchestration root.
*/
function createManifest(orchestrationDir, manifest) {
	const manifestPath = path$1.join(orchestrationDir, "manifest.json");
	fs$1.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2), "utf-8");
	const readmePath = path$1.join(orchestrationDir, "README.md");
	const readme = buildReadme(manifest);
	fs$1.writeFileSync(readmePath, readme, "utf-8");
}
/**
* Update a specific task entry in the manifest by index.
*/
function updateManifestTask(orchestrationDir, index, update) {
	const manifest = readManifest(orchestrationDir);
	if (!manifest) return;
	const task = manifest.tasks.find((t) => t.index === index);
	if (!task) return;
	Object.assign(task, update);
	const manifestPath = path$1.join(orchestrationDir, "manifest.json");
	fs$1.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2), "utf-8");
}
/**
* Set orchestration status and completedAt timestamp on the manifest.
*/
function completeManifest(orchestrationDir, status, completedAt) {
	const manifest = readManifest(orchestrationDir);
	if (!manifest) return;
	manifest.status = status;
	manifest.completedAt = completedAt ?? Date.now();
	const manifestPath = path$1.join(orchestrationDir, "manifest.json");
	fs$1.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2), "utf-8");
}
/**
* Read and parse manifest.json from orchestration directory.
* Returns null if missing or invalid.
*/
function readManifest(orchestrationDir) {
	const manifestPath = path$1.join(orchestrationDir, "manifest.json");
	try {
		const raw = fs$1.readFileSync(manifestPath, "utf-8");
		return JSON.parse(raw);
	} catch {
		return null;
	}
}
function buildReadme(manifest) {
	const lines = [];
	lines.push(`# Orchestration: ${manifest.orchestrationId}`);
	lines.push("");
	lines.push(`- **Mode:** ${manifest.mode}`);
	lines.push(`- **Status:** ${manifest.status}`);
	lines.push(`- **Started:** ${new Date(manifest.startedAt).toISOString()}`);
	if (manifest.completedAt) lines.push(`- **Completed:** ${new Date(manifest.completedAt).toISOString()}`);
	lines.push(`- **Tasks:** ${manifest.taskCount}`);
	lines.push("");
	lines.push("## Tasks");
	lines.push("");
	for (const task of manifest.tasks) {
		const templateSuffix = task.template ? ` (template: ${task.template})` : "";
		lines.push(`- [${task.status}] ${task.label}${templateSuffix}`);
	}
	lines.push("");
	return lines.join("\n");
}

//#endregion
export { updateManifestTask as i, createManifest as n, readManifest as r, completeManifest as t };