import { s as normalizeOptionalLowercaseString } from "./string-coerce-Bje8XVt9.js";
import { n as resolveOpenClawPackageRootSync } from "./openclaw-root-BizNJX65.js";
import { p as resolveUserPath } from "./utils-DvkbxKCZ.js";
import { fileURLToPath } from "node:url";
import fs from "node:fs";
import path from "node:path";
import os from "node:os";
//#region src/plugins/bundled-dir.ts
const DISABLED_BUNDLED_PLUGINS_DIR = path.join(os.tmpdir(), "openclaw-empty-bundled-plugins");
function areBundledPluginsDisabled(env = process.env) {
	const raw = normalizeOptionalLowercaseString(env.OPENCLAW_DISABLE_BUNDLED_PLUGINS);
	return raw === "1" || raw === "true";
}
function resolveDisabledBundledPluginsDir() {
	fs.mkdirSync(DISABLED_BUNDLED_PLUGINS_DIR, { recursive: true });
	return DISABLED_BUNDLED_PLUGINS_DIR;
}
function isSourceCheckoutRoot(packageRoot) {
	return fs.existsSync(path.join(packageRoot, ".git")) && fs.existsSync(path.join(packageRoot, "src")) && fs.existsSync(path.join(packageRoot, "extensions"));
}
function hasUsableBundledPluginTree(pluginsDir) {
	if (!fs.existsSync(pluginsDir)) return false;
	try {
		return fs.readdirSync(pluginsDir, { withFileTypes: true }).some((entry) => {
			if (!entry.isDirectory()) return false;
			const pluginDir = path.join(pluginsDir, entry.name);
			return fs.existsSync(path.join(pluginDir, "package.json")) || fs.existsSync(path.join(pluginDir, "openclaw.plugin.json"));
		});
	} catch {
		return false;
	}
}
function runningSourceTypeScriptProcess() {
	const argv1 = process.argv[1]?.toLowerCase();
	if (argv1?.endsWith(".ts") || argv1?.endsWith(".tsx") || argv1?.endsWith(".mts") || argv1?.endsWith(".cts")) return true;
	for (let index = 0; index < process.execArgv.length; index += 1) {
		const arg = process.execArgv[index]?.toLowerCase();
		if (!arg) continue;
		if (arg === "tsx" || arg.includes("tsx/register")) return true;
		if ((arg === "--import" || arg === "--loader") && process.execArgv[index + 1]) {
			const next = process.execArgv[index + 1].toLowerCase();
			if (next === "tsx" || next.includes("tsx/")) return true;
		}
	}
	return false;
}
function resolveBundledDirFromPackageRoot(packageRoot, preferSourceCheckout) {
	const sourceExtensionsDir = path.join(packageRoot, "extensions");
	const builtExtensionsDir = path.join(packageRoot, "dist", "extensions");
	const sourceCheckout = isSourceCheckoutRoot(packageRoot);
	if (preferSourceCheckout && fs.existsSync(sourceExtensionsDir)) return sourceExtensionsDir;
	const runtimeExtensionsDir = path.join(packageRoot, "dist-runtime", "extensions");
	const hasUsableRuntimeTree = sourceCheckout ? hasUsableBundledPluginTree(runtimeExtensionsDir) : fs.existsSync(runtimeExtensionsDir);
	const hasUsableBuiltTree = sourceCheckout ? hasUsableBundledPluginTree(builtExtensionsDir) : fs.existsSync(builtExtensionsDir);
	if (hasUsableRuntimeTree && hasUsableBuiltTree) return runtimeExtensionsDir;
	if (hasUsableBuiltTree) return builtExtensionsDir;
	if (sourceCheckout && fs.existsSync(sourceExtensionsDir)) return sourceExtensionsDir;
}
function resolveBundledPluginsDir(env = process.env) {
	if (areBundledPluginsDisabled(env)) return resolveDisabledBundledPluginsDir();
	const override = env.OPENCLAW_BUNDLED_PLUGINS_DIR?.trim();
	if (override) {
		const resolvedOverride = resolveUserPath(override, env);
		if (fs.existsSync(resolvedOverride)) return resolvedOverride;
		try {
			const argvPackageRoot = resolveOpenClawPackageRootSync({ argv1: process.argv[1] });
			if (argvPackageRoot && !isSourceCheckoutRoot(argvPackageRoot)) {
				const argvFallback = resolveBundledDirFromPackageRoot(argvPackageRoot, false);
				if (argvFallback) return argvFallback;
			}
		} catch {}
		return resolvedOverride;
	}
	const preferSourceCheckout = Boolean(env.VITEST) || runningSourceTypeScriptProcess();
	try {
		const argvRoot = resolveOpenClawPackageRootSync({ argv1: process.argv[1] });
		const cwdRoot = resolveOpenClawPackageRootSync({ cwd: process.cwd() });
		const moduleRoot = resolveOpenClawPackageRootSync({ moduleUrl: import.meta.url });
		const packageRoots = (preferSourceCheckout ? [
			cwdRoot,
			argvRoot,
			moduleRoot
		] : [
			argvRoot,
			cwdRoot,
			moduleRoot
		]).filter((entry, index, all) => Boolean(entry) && all.indexOf(entry) === index);
		for (const packageRoot of packageRoots) {
			const bundledDir = resolveBundledDirFromPackageRoot(packageRoot, preferSourceCheckout);
			if (bundledDir) return bundledDir;
		}
	} catch {}
	try {
		const execDir = path.dirname(process.execPath);
		const siblingBuilt = path.join(execDir, "dist", "extensions");
		if (fs.existsSync(siblingBuilt)) return siblingBuilt;
		const sibling = path.join(execDir, "extensions");
		if (fs.existsSync(sibling)) return sibling;
	} catch {}
	try {
		let cursor = path.dirname(fileURLToPath(import.meta.url));
		for (let i = 0; i < 6; i += 1) {
			const candidate = path.join(cursor, "extensions");
			if (fs.existsSync(candidate)) return candidate;
			const parent = path.dirname(cursor);
			if (parent === cursor) break;
			cursor = parent;
		}
	} catch {}
}
//#endregion
export { resolveBundledPluginsDir as n, areBundledPluginsDisabled as t };
