import { SFNoProjectError } from "./context.js";
import { handleAutonomousCommand } from "./handlers/autonomous.js";
import { handleCoreCommand } from "./handlers/core.js";
import { handleOpsCommand } from "./handlers/ops.js";
import { handleParallelCommand } from "./handlers/parallel.js";
import { handleWorkflowCommand } from "./handlers/workflow.js";
export async function handleSFCommand(args, ctx, pi) {
	const trimmed = (typeof args === "string" ? args : "").trim();
	const handlers = [
		() => handleCoreCommand(trimmed, ctx, pi),
		() => handleAutonomousCommand(trimmed, ctx, pi),
		() => handleParallelCommand(trimmed, ctx, pi),
		() => handleWorkflowCommand(trimmed, ctx, pi),
		() => handleOpsCommand(trimmed, ctx, pi),
	];
	try {
		for (const handler of handlers) {
			if (await handler()) {
				return;
			}
		}
	} catch (err) {
		if (err instanceof SFNoProjectError) {
			ctx.ui.notify(
				`${err.message} \`cd\` into a project directory first.`,
				"warning",
			);
			return;
		}
		throw err;
	}
	ctx.ui.notify(
		`Unknown: /${trimmed}. Run /help for available commands.`,
		"warning",
	);
}
