import { getErrorMessage } from "./error-utils.js";

export {
	clearPendingGate,
	getPendingGate,
	isDepthConfirmationAnswer,
	isDepthVerified,
	isGateQuestionId,
	isQueuePhaseActive,
	setPendingGate,
	setQueuePhaseActive,
	shouldBlockContextWrite,
	shouldBlockPendingGate,
	shouldBlockPendingGateBash,
	shouldBlockQueueExecution,
} from "./bootstrap/write-gate.js";
export default async function registerExtension(pi) {
	// Always register the core SF commands first, in isolation.
	// This ensures commands such as /autonomous and /plan are available even if
	// the full bootstrap (shortcuts,
	// tools, hooks) fails — e.g. due to a Windows-specific import error.
	const { registerSFCommands } = await import("./commands/index.js");
	registerSFCommands(pi);
	const { registerChatCommandRouter } = await import(
		"./chat-command-router.js"
	);
	registerChatCommandRouter(pi);

	// Register steerable autonomous extension for Copilot Auto-style controls
	const { default: steerableAutonomousExtension } = await import(
		"./steerable-autonomous-extension.js"
	);
	steerableAutonomousExtension(pi);

	// Full setup (shortcuts, tools, hooks) in a separate try/catch so that
	// any platform-specific load failure doesn't take out the core command.
	try {
		const { registerSfExtension } = await import(
			"./bootstrap/register-extension.js"
		);
		registerSfExtension(pi);
	} catch (err) {
		const { logWarning } = await import("./workflow-logger.js");
		logWarning(
			"bootstrap",
			`Extension setup partially failed — SF commands are available but shortcuts/tools may be missing: ${getErrorMessage(err)}`,
		);
	}

	// Register SF-owned subagent tools after core bootstrap so model, preference,
	// inheritance, and retrieval-evidence integrations share one extension owner.
	try {
		const { default: registerSFSubagents } = await import(
			"./subagent/index.js"
		);
		registerSFSubagents(pi);
	} catch (err) {
		const { logWarning } = await import("./workflow-logger.js");
		logWarning(
			"subagent",
			`SF subagent setup failed — delegation tools may be missing: ${getErrorMessage(err)}`,
		);
	}

	// Register SF TUI chrome (header, footer, prompt history, shortcuts, tools).
	// This was previously a separate sf-tui bundled extension; folded here because
	// every feature has a hard dependency on sf/ internals (auto session, project root).
	try {
		const { default: registerSFTUI } = await import("./ui/index.js");
		registerSFTUI(pi);
	} catch (err) {
		const { logWarning } = await import("./workflow-logger.js");
		logWarning(
			"ui",
			`SF TUI setup failed — running with default header/footer: ${getErrorMessage(err)}`,
		);
	}

	// Register SF usage bar (/usage command — provider usage windows + status).
	try {
		const { default: registerSFUsageBar } = await import("./ui/usage-bar.js");
		registerSFUsageBar(pi);
	} catch (err) {
		const { logWarning } = await import("./workflow-logger.js");
		logWarning(
			"usage-bar",
			`SF usage bar setup failed: ${getErrorMessage(err)}`,
		);
	}

	// Register SF notifications (completion beep/say/focus/threshold commands).
	try {
		const { default: registerSFNotify } = await import(
			"./notifications/notify.js"
		);
		registerSFNotify(pi);
	} catch (err) {
		const { logWarning } = await import("./workflow-logger.js");
		logWarning(
			"notifications",
			`SF notifications setup failed: ${getErrorMessage(err)}`,
		);
	}

	// Register SF in-turn guard (duplicate tool-call loop detection).
	try {
		const { default: registerSFInturnGuard } = await import(
			"./guards/inturn.js"
		);
		registerSFInturnGuard(pi);
	} catch (err) {
		const { logWarning } = await import("./workflow-logger.js");
		logWarning(
			"inturn-guard",
			`SF in-turn guard setup failed: ${getErrorMessage(err)}`,
		);
	}

	// Register SF permissions (layered permission enforcement).
	try {
		const { default: registerSFPermissions } = await import(
			"./permissions/index.js"
		);
		registerSFPermissions(pi);
	} catch (err) {
		const { logWarning } = await import("./workflow-logger.js");
		logWarning(
			"permissions",
			`SF permissions setup failed: ${getErrorMessage(err)}`,
		);
	}

	// Register SF legacy slash commands (/audit, /clear, /create-extension, /create-slash-command).
	try {
		const { default: registerSFLegacyCommands } = await import(
			"./commands/legacy/index.js"
		);
		registerSFLegacyCommands(pi);
	} catch (err) {
		const { logWarning } = await import("./workflow-logger.js");
		logWarning(
			"legacy-commands",
			`SF legacy commands setup failed: ${getErrorMessage(err)}`,
		);
	}
}
