import "./paths-Ci0O5IXU.js";
import "./globals-o4Hkf2nH.js";
import "./utils-DXDZXfi2.js";
import "./agent-scope-BUcFoOez.js";
import "./subsystem-CocLsF9M.js";
import "./core-root-D_XTmw6_.js";
import "./logger-B4_TThxB.js";
import "./exec-Bvzmovpq.js";
import "./model-selection-YEFtLNSF.js";
import "./registry-CZfQ-snX.js";
import "./github-copilot-token-DqwKrrmK.js";
import "./boolean-C6Pbt2Ue.js";
import "./env-D3eA4r2n.js";
import "./manifest-registry-CqRtX07d.js";
import "./dock-B9SrGH_K.js";
import "./message-channel-B7if8_Ss.js";
import "./plugins-DPuCvyAr.js";
import "./sessions-CxtmRBrc.js";
import "./tailscale-BxdRBeK_.js";
import "./tailnet-DONkFGUG.js";
import "./ws-DQm2qOSB.js";
import "./auth-BfvQ8QYP.js";
import "./accounts--YCaC3h2.js";
import "./accounts-DIXxXacs.js";
import "./logging-w5jq5901.js";
import "./accounts-Cy5Z48cS.js";
import "./paths-DyjDYWsu.js";
import "./chat-envelope-DjXKeY_Z.js";
import "./client-DoEmtGyQ.js";
import "./call-CGx2d27E.js";
import "./pairing-token-BbiCPOiP.js";
import "./onboard-helpers-CgBGHcnn.js";
import "./prompt-style-C3CFmIhm.js";
import "./runtime-guard-DRPIdCHk.js";
import "./note-BxgUVS4k.js";
import { n as gatewayInstallErrorHint, t as buildGatewayInstallPlan } from "./daemon-install-helpers-BaAKOoka.js";
import { r as isGatewayDaemonRuntime, t as DEFAULT_GATEWAY_DAEMON_RUNTIME } from "./daemon-runtime-CMnbwryP.js";
import { t as resolveGatewayInstallToken } from "./gateway-install-token-Con7-XZK.js";
import { r as isSystemdUserServiceAvailable } from "./systemd-Coag6AD6.js";
import { t as resolveGatewayService } from "./service-BuILYYFR.js";
import { n as ensureSystemdUserLingerNonInteractive } from "./systemd-linger-DIcLDkk6.js";

//#region src/commands/onboard-non-interactive/local/daemon-install.ts
async function installGatewayDaemonNonInteractive(params) {
	const { opts, runtime, port } = params;
	if (!opts.installDaemon) return;
	const daemonRuntimeRaw = opts.daemonRuntime ?? DEFAULT_GATEWAY_DAEMON_RUNTIME;
	const systemdAvailable = process.platform === "linux" ? await isSystemdUserServiceAvailable() : true;
	if (process.platform === "linux" && !systemdAvailable) {
		runtime.log("Systemd user services are unavailable; skipping service install.");
		return;
	}
	if (!isGatewayDaemonRuntime(daemonRuntimeRaw)) {
		runtime.error("Invalid --daemon-runtime (use node or bun)");
		runtime.exit(1);
		return;
	}
	const service = resolveGatewayService();
	const tokenResolution = await resolveGatewayInstallToken({
		config: params.nextConfig,
		env: process.env
	});
	for (const warning of tokenResolution.warnings) runtime.log(warning);
	if (tokenResolution.unavailableReason) {
		runtime.error([
			"Gateway install blocked:",
			tokenResolution.unavailableReason,
			"Fix gateway auth config/token input and rerun onboarding."
		].join(" "));
		runtime.exit(1);
		return;
	}
	const { programArguments, workingDirectory, environment } = await buildGatewayInstallPlan({
		env: process.env,
		port,
		token: tokenResolution.token,
		runtime: daemonRuntimeRaw,
		warn: (message) => runtime.log(message),
		config: params.nextConfig
	});
	try {
		await service.install({
			env: process.env,
			stdout: process.stdout,
			programArguments,
			workingDirectory,
			environment
		});
	} catch (err) {
		runtime.error(`Gateway service install failed: ${String(err)}`);
		runtime.log(gatewayInstallErrorHint());
		return;
	}
	await ensureSystemdUserLingerNonInteractive({ runtime });
}

//#endregion
export { installGatewayDaemonNonInteractive };