import { loadConfig } from "../config/loader.js";
import { collectConfiguredChannelIds } from "../extensions/activation-context.js";
import { getWorkspacePath } from "../config/workspace-path-helpers.js";
import { ExtensionLoader } from "../extensions/loader.js";
import "../extensions/index.js";
import { MessageBus } from "../infra/bus/queue.js";
import "../infra/bus/index.js";
import { createDefaultContext } from "./registry.js";
import "../config/index.js";
import { registerExtensionCliProgram } from "../extensions/sdk/channel-helpers.js";
import { join } from "path";
//#region src/cli/extension-cli-register.ts
function argvRequestsVersionOnly(argv) {
	return argv.includes("--version") || argv.includes("-V");
}
/**
* Load extensions that may register CLI commands (manifest-only skip when nothing would activate).
*/
async function registerExtensionCliCommands(program) {
	if (argvRequestsVersionOnly(process.argv)) return;
	const ctx = createDefaultContext(process.argv, {});
	const config = loadConfig(ctx.configPath);
	const workspace = getWorkspacePath(config) || ctx.workspacePath;
	const rawExt = config.extensions;
	const extBlock = rawExt && typeof rawExt === "object" && !Array.isArray(rawExt) ? rawExt : void 0;
	const enabledIds = Array.isArray(extBlock?.enabled) ? extBlock.enabled.filter((x) => typeof x === "string") : [];
	const bus = new MessageBus();
	const loader = new ExtensionLoader({
		workspaceDir: workspace,
		extensionsDir: join(workspace, ".extensions")
	});
	loader.setConfig(config);
	const envWouldActivate = loader.buildManifestRegistry().detectAvailableByEnv(process.env).length > 0;
	const channelWouldActivate = (collectConfiguredChannelIds(config)?.length ?? 0) > 0;
	if (enabledIds.length === 0 && !channelWouldActivate && !envWouldActivate) return;
	loader.setRuntimeContext({ bus });
	await loader.loadByActivationPlan();
	registerExtensionCliProgram(program, loader.getRegistry());
}
//#endregion
export { registerExtensionCliCommands };

//# sourceMappingURL=extension-cli-register.js.map