const require_logger = require('./logger-B3t6CmTC.cjs');
const require_esm = require('./esm-DRsi2FRh.cjs');
let fs = require("fs");
fs = require_logger.__toESM(fs);
let path = require("path");
path = require_logger.__toESM(path);
let os = require("os");
os = require_logger.__toESM(os);
let child_process = require("child_process");
let util = require("util");
let python_shell = require("python-shell");

//#region src/python/pythonUtils.ts
const execFileAsync = (0, util.promisify)(child_process.execFile);
/**
* Gets an integer value from an environment variable.
* @param key - The environment variable name
* @returns The parsed integer value, or undefined if not set or not a valid integer
*/
function getEnvInt(key) {
	const value = process.env[key];
	if (value === void 0) return;
	const parsed = parseInt(value, 10);
	return isNaN(parsed) ? void 0 : parsed;
}
const state = {
	cachedPythonPath: null,
	validationPromise: null
};
/**
* Try to find Python using Windows 'where' command, filtering out Microsoft Store stubs.
*/
async function tryWindowsWhere() {
	try {
		const output = (await execFileAsync("where", ["python"])).stdout.trim();
		if (!output) {
			require_logger.logger_default.debug("Windows 'where python' returned empty output");
			return null;
		}
		const paths = output.split("\n").filter((path$2) => path$2.trim());
		for (const pythonPath of paths) {
			const trimmedPath = pythonPath.trim();
			if (trimmedPath.includes("WindowsApps") || !trimmedPath.endsWith(".exe")) continue;
			const validated = await tryPath(trimmedPath);
			if (validated) return validated;
		}
	} catch (error) {
		const errorMsg = error instanceof Error ? error.message : String(error);
		require_logger.logger_default.debug(`Windows 'where python' failed: ${errorMsg}`);
		if (errorMsg.includes("Access is denied") || errorMsg.includes("EACCES")) require_logger.logger_default.warn(`Permission denied when searching for Python: ${errorMsg}`);
	}
	return null;
}
/**
* Try Python commands to get sys.executable path.
*/
async function tryPythonCommands(commands) {
	for (const cmd of commands) try {
		const executablePath = (await execFileAsync(cmd, ["-c", "import sys; print(sys.executable)"])).stdout.trim();
		if (executablePath && executablePath !== "None") {
			if (process.platform === "win32" && !executablePath.toLowerCase().endsWith(".exe")) {
				if (executablePath.includes("\\") || /^[A-Za-z]:/.test(executablePath)) return executablePath + ".exe";
			}
			return executablePath;
		}
	} catch (error) {
		const errorMsg = error instanceof Error ? error.message : String(error);
		require_logger.logger_default.debug(`Python command "${cmd}" failed: ${errorMsg}`);
		if (errorMsg.includes("Access is denied") || errorMsg.includes("EACCES") || errorMsg.includes("EPERM")) require_logger.logger_default.warn(`Permission denied when trying Python command "${cmd}": ${errorMsg}`);
	}
	return null;
}
/**
* Try direct command validation as final fallback.
*/
async function tryDirectCommands(commands) {
	for (const cmd of commands) try {
		const validated = await tryPath(cmd);
		if (validated) return validated;
	} catch (error) {
		const errorMsg = error instanceof Error ? error.message : String(error);
		require_logger.logger_default.debug(`Direct command "${cmd}" failed: ${errorMsg}`);
		if (errorMsg.includes("Access is denied") || errorMsg.includes("EACCES") || errorMsg.includes("EPERM")) require_logger.logger_default.warn(`Permission denied when trying Python command "${cmd}": ${errorMsg}`);
	}
	return null;
}
/**
* Attempts to get the Python executable path using platform-appropriate strategies.
* @returns The Python executable path if successful, or null if failed.
*/
async function getSysExecutable() {
	if (process.platform === "win32") {
		const whereResult = await tryWindowsWhere();
		if (whereResult) return whereResult;
		const sysResult = await tryPythonCommands(["py", "py -3"]);
		if (sysResult) return sysResult;
		return await tryDirectCommands(["python"]);
	} else return await tryPythonCommands(["python3", "python"]);
}
/**
* Attempts to validate a Python executable path.
* @param path - The path to the Python executable to test.
* @returns The validated path if successful, or null if invalid.
*/
async function tryPath(path$2) {
	let timeoutId;
	try {
		const timeoutPromise = new Promise((_, reject) => {
			timeoutId = setTimeout(() => reject(/* @__PURE__ */ new Error("Command timed out")), 2500);
		});
		const result = await Promise.race([execFileAsync(path$2, ["--version"]), timeoutPromise]);
		if (timeoutId) clearTimeout(timeoutId);
		if (result.stdout.trim().startsWith("Python")) return path$2;
		return null;
	} catch {
		if (timeoutId) clearTimeout(timeoutId);
		return null;
	}
}
/**
* Validates and caches the Python executable path.
*
* @param pythonPath - Path to the Python executable.
* @param isExplicit - If true, only tries the provided path.
* @returns Validated Python executable path.
* @throws {Error} If no valid Python executable is found.
*/
async function validatePythonPath(pythonPath, isExplicit) {
	if (state.cachedPythonPath) return state.cachedPythonPath;
	if (!state.validationPromise) state.validationPromise = (async () => {
		try {
			const primaryPath = await tryPath(pythonPath);
			if (primaryPath) {
				state.cachedPythonPath = primaryPath;
				state.validationPromise = null;
				return primaryPath;
			}
			if (isExplicit) {
				const error$1 = /* @__PURE__ */ new Error(`Python 3 not found. Tried "${pythonPath}" Please ensure Python 3 is installed and set the PROMPTFOO_PYTHON environment variable to your Python 3 executable path (e.g., '${process.platform === "win32" ? "C:\\Python39\\python.exe" : "/usr/bin/python3"}').`);
				state.validationPromise = null;
				throw error$1;
			}
			const detectedPath = await getSysExecutable();
			if (detectedPath) {
				state.cachedPythonPath = detectedPath;
				state.validationPromise = null;
				return detectedPath;
			}
			const error = /* @__PURE__ */ new Error(`Python 3 not found. Tried "${pythonPath}", sys.executable detection, and fallback commands. Please ensure Python 3 is installed and set the PROMPTFOO_PYTHON environment variable to your Python 3 executable path (e.g., '${process.platform === "win32" ? "C:\\Python39\\python.exe" : "/usr/bin/python3"}').`);
			state.validationPromise = null;
			throw error;
		} catch (error) {
			state.validationPromise = null;
			throw error;
		}
	})();
	return state.validationPromise;
}
/**
* Runs a Python script with the specified method and arguments.
*
* @param scriptPath - The path to the Python script to run.
* @param method - The name of the method to call in the Python script.
* @param args - An array of arguments to pass to the Python script.
* @param options - Optional settings for running the Python script.
* @param options.pythonExecutable - Optional path to the Python executable.
* @returns A promise that resolves to the output of the Python script.
* @throws An error if there's an issue running the Python script or parsing its output.
*/
async function runPython(scriptPath, method, args, options = {}) {
	const absPath = path.default.resolve(scriptPath);
	const tempJsonPath = path.default.join(os.default.tmpdir(), `promptfoo-python-input-json-${Date.now()}-${Math.random().toString(16).slice(2)}.json`);
	const outputPath = path.default.join(os.default.tmpdir(), `promptfoo-python-output-json-${Date.now()}-${Math.random().toString(16).slice(2)}.json`);
	const customPath = options.pythonExecutable || require_logger.getEnvString("PROMPTFOO_PYTHON");
	let pythonPath = customPath || "python";
	pythonPath = await validatePythonPath(pythonPath, typeof customPath === "string");
	const pythonOptions = {
		args: [
			absPath,
			method,
			tempJsonPath,
			outputPath
		],
		env: process.env,
		mode: "binary",
		pythonPath,
		scriptPath: require_esm.getWrapperDir("python"),
		...require_logger.getEnvBool("PROMPTFOO_PYTHON_DEBUG_ENABLED") && { stdio: "inherit" }
	};
	try {
		fs.default.writeFileSync(tempJsonPath, require_logger.safeJsonStringify(args), "utf-8");
		require_logger.logger_default.debug(`Running Python wrapper with args: ${require_logger.safeJsonStringify(args)}`);
		await new Promise((resolve, reject) => {
			try {
				const pyshell = new python_shell.PythonShell("wrapper.py", pythonOptions);
				pyshell.stdout?.on("data", (chunk) => {
					require_logger.logger_default.debug(chunk.toString("utf-8").trim());
				});
				pyshell.stderr?.on("data", (chunk) => {
					require_logger.logger_default.error(chunk.toString("utf-8").trim());
				});
				pyshell.end((err) => {
					if (err) reject(err);
					else resolve();
				});
			} catch (error) {
				reject(error);
			}
		});
		const output = fs.default.readFileSync(outputPath, "utf-8");
		require_logger.logger_default.debug(`Python script ${absPath} returned: ${output}`);
		let result;
		try {
			result = JSON.parse(output);
			require_logger.logger_default.debug(`Python script ${absPath} parsed output type: ${typeof result}, structure: ${result ? JSON.stringify(Object.keys(result)) : "undefined"}`);
		} catch (error) {
			throw new Error(`Invalid JSON: ${error.message} when parsing result: ${output}\nStack Trace: ${error.stack}`);
		}
		if (result?.type !== "final_result") throw new Error("The Python script `call_api` function must return a dict with an `output`");
		return result.data;
	} catch (error) {
		require_logger.logger_default.error(`Error running Python script: ${error.message}\nStack Trace: ${error.stack?.replace("--- Python Traceback ---", "Python Traceback: ") || "No Python traceback available"}`);
		throw new Error(`Error running Python script: ${error.message}\nStack Trace: ${error.stack?.replace("--- Python Traceback ---", "Python Traceback: ") || "No Python traceback available"}`);
	} finally {
		[tempJsonPath, outputPath].forEach((file) => {
			try {
				fs.default.unlinkSync(file);
			} catch (error) {
				require_logger.logger_default.error(`Error removing ${file}: ${error}`);
			}
		});
	}
}

//#endregion
Object.defineProperty(exports, 'getEnvInt', {
  enumerable: true,
  get: function () {
    return getEnvInt;
  }
});
Object.defineProperty(exports, 'runPython', {
  enumerable: true,
  get: function () {
    return runPython;
  }
});
Object.defineProperty(exports, 'validatePythonPath', {
  enumerable: true,
  get: function () {
    return validatePythonPath;
  }
});
//# sourceMappingURL=pythonUtils-BU2j0p3m.cjs.map