"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.js');
const require_bin_which = require('../bin/which.js');
const require_process_spawn_child = require('../process/spawn/child.js');
const require_fs_safe = require('../fs/safe.js');
let node_fs = require("node:fs");
let node_path = require("node:path");
node_path = require_runtime.__toESM(node_path);
let node_process = require("node:process");
node_process = require_runtime.__toESM(node_process);

//#region src/external-tools/from-pip-venv.ts
/**
* @file Generic "venv-install tier" for external-tools resolvers. Parallel to
*   `from-download.ts` — that one handles single-binary tools downloaded as
*   GitHub release assets; this one handles Python packages installed into a
*   single-purpose venv. Per-tool resolvers (skillspector, future Python CLIs)
*   compose their fourth tier on top of these helpers. Two helpers:
*
*   - `createPipVenv` — `python -m venv <cacheDir>` + `pip install <spec>`.
*     Idempotent: hits the existing venv when its entry-point already exists.
*     Stops at "venv created, entry-point present."
*   - `findPython` — locates `python3` (or `python` on Windows) on PATH. Returns
*     the absolute path or `undefined` when no Python is available. What this
*     does NOT do:
*   - Decide where the venv lives. The caller picks the cache dir (typically
*     under `getSocketDlxDir()`); we just create the venv there.
*   - Verify package integrity beyond pip's own wheel-hash mechanism. Pinning is
*     the caller's responsibility: pass exact-version `<pkg>==<ver>` or git-SHA
*     `git+...@<sha>`.
*   - Re-resolve when the cache is stale. The cache key is derived by the caller
*     from the install spec; a re-pin produces a new dir.
*/
/**
* Create (or reuse) a venv at `cacheDir` and pip-install `installSpec` into it.
* Returns the entry-point path + a `created` flag. Throws when:
*
* - No Python interpreter is on PATH (and none was passed via `python`).
* - `python -m venv` fails.
* - `pip install` fails.
* - The install succeeded but the entry-point was not created (caller passed a
*   wrong `entryPoint` name, or the package has no console script).
*/
async function createPipVenv(opts) {
	const { cacheDir, entryPoint, installSpec } = opts;
	const entryBin = pipVenvEntryPointPath(cacheDir, entryPoint);
	if ((0, node_fs.existsSync)(entryBin)) return {
		entryPointPath: entryBin,
		created: false
	};
	const python = opts.python ?? await findPython();
	if (!python) throw new Error("createPipVenv: no Python interpreter on PATH (looked for python3, python)");
	await require_fs_safe.safeMkdir(node_path.default.dirname(cacheDir), { recursive: true });
	await require_process_spawn_child.spawn(python, [
		"-m",
		"venv",
		"--clear",
		cacheDir
	], { stdio: "pipe" });
	const venvPython = pipVenvEntryPointPath(cacheDir, "python");
	if (!(0, node_fs.existsSync)(venvPython)) throw new Error(`createPipVenv: venv created at ${cacheDir} but ${venvPython} is missing`);
	await require_process_spawn_child.spawn(venvPython, [
		"-m",
		"pip",
		"install",
		"--no-input",
		"--disable-pip-version-check",
		installSpec
	], { stdio: "pipe" });
	if (!(0, node_fs.existsSync)(entryBin)) throw new Error(`createPipVenv: pip install ${installSpec} succeeded but entry-point ${entryPoint} was not created at ${entryBin}`);
	return {
		entryPointPath: entryBin,
		created: true
	};
}
/**
* Locate a Python interpreter. Prefer `python3` on macOS/Linux, fall back to
* `python` (the Windows convention). Returns `undefined` when neither is on
* PATH.
*/
async function findPython() {
	const candidates = node_process.default.platform === "win32" ? ["python", "python3"] : ["python3", "python"];
	for (let i = 0, { length } = candidates; i < length; i += 1) {
		const found = await require_bin_which.which(candidates[i], { nothrow: true });
		if (typeof found === "string") return found;
	}
}
function pipVenvEntryPointPath(venvDir, entryPoint) {
	if (node_process.default.platform === "win32") return node_path.default.join(venvDir, "Scripts", `${entryPoint}.exe`);
	return node_path.default.join(venvDir, "bin", entryPoint);
}

//#endregion
exports.createPipVenv = createPipVenv;
exports.findPython = findPython;
exports.pipVenvEntryPointPath = pipVenvEntryPointPath;