#!/usr/bin/env node
import { i as formatUncaughtError } from "./errors-CGMew-1w.js";
import { t as isMainModule } from "./is-main-EuRWM-HL.js";
import { t as installUnhandledRejectionHandler } from "./unhandled-rejections-DS4dzceq.js";
import process from "node:process";
import { fileURLToPath } from "node:url";
//#region src/index.ts
let applyTemplate;
let createDefaultDeps;
let deriveSessionKey;
let describePortOwner;
let ensureBinary;
let ensurePortAvailable;
let getReplyFromConfig;
let handlePortError;
let loadConfig;
let loadSessionStore;
let monitorWebChannel;
let normalizeE164;
let PortInUseError;
let promptYesNo;
let resolveSessionKey;
let resolveStorePath;
let runCommandWithTimeout;
let runExec;
let saveSessionStore;
let waitForever;
async function loadLegacyCliDeps() {
	const [{ installGaxiosFetchCompat }, { runCli }] = await Promise.all([import("./gaxios-fetch-compat-jL2xxHMj.js"), import("./run-main-D-du7tud.js")]);
	return {
		installGaxiosFetchCompat,
		runCli
	};
}
async function runLegacyCliEntry(argv = process.argv, deps) {
	const { installGaxiosFetchCompat, runCli } = deps ?? await loadLegacyCliDeps();
	await installGaxiosFetchCompat();
	await runCli(argv);
}
const isMain = isMainModule({ currentFile: fileURLToPath(import.meta.url) });
if (!isMain) ({applyTemplate, createDefaultDeps, deriveSessionKey, describePortOwner, ensureBinary, ensurePortAvailable, getReplyFromConfig, handlePortError, loadConfig, loadSessionStore, monitorWebChannel, normalizeE164, PortInUseError, promptYesNo, resolveSessionKey, resolveStorePath, runCommandWithTimeout, runExec, saveSessionStore, waitForever} = await import("./library-DvOzfjwB.js"));
if (isMain) {
	const { restoreTerminalState } = await import("./restore-ADlpnb-M.js");
	installUnhandledRejectionHandler();
	process.on("uncaughtException", (error) => {
		console.error("[kaijibot] Uncaught exception:", formatUncaughtError(error));
		restoreTerminalState("uncaught exception", { resumeStdinIfPaused: false });
		process.exit(1);
	});
	runLegacyCliEntry(process.argv).catch((err) => {
		console.error("[kaijibot] CLI failed:", formatUncaughtError(err));
		restoreTerminalState("legacy cli failure", { resumeStdinIfPaused: false });
		process.exit(1);
	});
}
//#endregion
export { PortInUseError, applyTemplate, createDefaultDeps, deriveSessionKey, describePortOwner, ensureBinary, ensurePortAvailable, getReplyFromConfig, handlePortError, loadConfig, loadSessionStore, monitorWebChannel, normalizeE164, promptYesNo, resolveSessionKey, resolveStorePath, runCommandWithTimeout, runExec, runLegacyCliEntry, saveSessionStore, waitForever };
