import "./subsystem-hATE68va.js";
import "./paths-VdmjGGyc.js";
import "./theme-C5EwWDWE.js";
import "./ansi-By7x1TI7.js";
import "./logger-BKidfKRU.js";
import "./exec-XH65-wH1.js";
import { t as VERSION } from "./version-BKRSzdyx.js";
import "./restart-sentinel-BqQzvk3k.js";
import { c as compareSemverStrings, l as fetchNpmLatestVersion, t as runGatewayUpdate } from "./update-runner-BF2ZUZuV.js";
import "./control-ui-assets-C6cSlkkc.js";
import process from "node:process";
import { existsSync, realpathSync } from "node:fs";
import { dirname, join } from "node:path";

//#region src/infra/cli-auto-update.ts
/**
* @module infra/cli-auto-update
*
* Best-effort auto-update on CLI startup: if a newer version is published,
* install it so the NEXT `koi` invocation is current. Non-blocking and silent.
*
* Real installs only — a developer/git checkout updates via the repo, so we
* never auto-`npm install` over it. This complements the gateway's own 5s
* auto-update loop: it covers the case where no gateway is running yet (e.g. the
* very first `koi` after install, or a CLI-only machine).
*/
/** A git/dev checkout updates via the repo, never via auto npm install. */
function isDevCheckout() {
	try {
		const main = process.argv[1];
		const dir = main ? dirname(realpathSync(main)) : null;
		return Boolean(dir && existsSync(join(dir, ".git")));
	} catch {
		return false;
	}
}
/** Fire-and-forget; resolves quickly when already current. Never throws. */
async function maybeAutoUpdateOnStartup() {
	if (process.env.SKYKOI_NO_AUTO_UPDATE?.trim()) return;
	if (isDevCheckout()) return;
	try {
		const latest = (await fetchNpmLatestVersion({ timeoutMs: 4e3 }))?.latestVersion;
		if (!latest) return;
		if ((compareSemverStrings(latest, VERSION) ?? 0) <= 0) return;
		await runGatewayUpdate({ tag: "latest" });
	} catch {}
}

//#endregion
export { maybeAutoUpdateOnStartup };