import { i as __require } from "./chunk-CrysgU_F.js";
import { existsSync } from "node:fs";
import { basename, dirname, join } from "node:path";
import { execSync } from "node:child_process";
//#region src/infra/lark-cli/resolve.ts
let cachedPath = null;
/**
* Resolve the lark-cli binary path.
*
* The @larksuite/cli package has no main/exports — it ships a Go binary
* behind a Node wrapper at `scripts/run.js`. Resolution strategy:
*
* 1. Try `require.resolve("@larksuite/cli/scripts/run.js")` — the Node wrapper
* 2. Fall back to `lark-cli` in $PATH via `which`
*
* Result is cached after the first call. Returns `undefined` if not available.
*/
function resolveLarkCliPath() {
	if (cachedPath !== null) return cachedPath;
	try {
		const runJs = __require.resolve("@larksuite/cli/scripts/run.js");
		cachedPath = runJs;
		return runJs;
	} catch {}
	try {
		const result = execSync(process.platform === "win32" ? "where lark-cli" : "which lark-cli", {
			encoding: "utf-8",
			timeout: 3e3
		}).trim();
		if (result) {
			cachedPath = result.split("\n")[0].trim();
			return cachedPath;
		}
	} catch {}
	cachedPath = void 0;
}
/**
* Check if lark-cli is available (binary can be resolved).
*/
function isLarkCliAvailable() {
	return resolveLarkCliPath() !== void 0;
}
/**
* Derive the `node_modules/.bin` directory containing the lark-cli symlink.
* Returns `undefined` if lark-cli is not installed or `.bin` doesn't exist.
*/
function resolveLarkCliBinDir() {
	const binPath = resolveLarkCliPath();
	if (!binPath) return;
	let dir = dirname(binPath);
	while (dir !== dirname(dir)) {
		if (basename(dir) === "node_modules") {
			const binDir = join(dir, ".bin");
			return existsSync(binDir) ? binDir : void 0;
		}
		dir = dirname(dir);
	}
}
//#endregion
export { resolveLarkCliBinDir as n, resolveLarkCliPath as r, isLarkCliAvailable as t };
