import type {
	ExtensionAPI,
	ExtensionCommandContext,
} from "@singularity-forge/pi-coding-agent";
import { importExtensionModule } from "@singularity-forge/pi-coding-agent";

import { getSfArgumentCompletions, SF_COMMAND_DESCRIPTION } from "./catalog.js";

export function registerSFCommand(pi: ExtensionAPI): void {
	pi.registerCommand("sf", {
		description: SF_COMMAND_DESCRIPTION,
		getArgumentCompletions: getSfArgumentCompletions,
		handler: async (args: string, ctx: ExtensionCommandContext) => {
			const { handleSFCommand } = await importExtensionModule<
				typeof import("./dispatcher.js")
			>(import.meta.url, "./dispatcher.js");
			const { setStderrLoggingEnabled } = await importExtensionModule<
				typeof import("../workflow-logger.js")
			>(import.meta.url, "../workflow-logger.js");
			const previousStderrSetting = setStderrLoggingEnabled(false);
			try {
				await handleSFCommand(args, ctx, pi);
			} finally {
				setStderrLoggingEnabled(previousStderrSetting);
			}
		},
	});
}
