"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../../_virtual/_rolldown/runtime.js');
const require_primordials_string = require('../../primordials/string.js');
const require_paths_socket = require('../../paths/socket.js');
const require_external_tools_from_download = require('../from-download.js');
const require_external_tools_jre_asset_names = require('./asset-names.js');
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/jre/from-download.ts
/**
* @file `jreFromDownload()` — fetches Adoptium JRE and returns a `ResolvedJre`
*   pointing at the extracted java binary. Wraps the generic
*   `downloadAndExtractTool` with JRE-specific knowledge:
*
*   - URL construction via `getAdoptiumDownloadUrl`
*   - default cache layout: `<dlxDir>/jre/<version>/<platform-arch>/`
*   - locating `bin/java(.exe)` within the extracted tree Returns `undefined`
*     when:
*   - Adoptium has no build for the target `platformArch`
*   - The extracted tree doesn't contain a recognizable JRE layout Idempotent —
*     re-running with the same `{version, platformArch}` after a successful
*     first run is a quick cache check, no network. Trust-on-first-use: pass
*     `integrity` from `external-tools.json` when available. Omit it on first
*     install; the helper logs the computed SRI through `result.integrity`
*     (currently surfaced only as a side effect of the underlying call; future
*     revision may surface it via an out-param so consumers can record the
*     pin).
*/
/**
* Resolve a JRE by downloading it from Adoptium. Returns the standard
* `ResolvedJre` shape with `source: 'download'`.
*
* @example
*   ;```typescript
*   const jre = await jreFromDownload({
*     version: 21,
*     platformArch: 'darwin-arm64',
*   })
*   // → { javaPath: '/.../bin/java', javaHome: '/...', source: 'download' }
*   ```
*/
async function jreFromDownload(opts) {
	const { cacheDir, downloader, integrity, platformArch, version } = opts;
	const url = require_external_tools_jre_asset_names.getAdoptiumDownloadUrl({
		version,
		platformArch
	});
	if (!url) return;
	const extractedDir = cacheDir ?? node_path.default.join(require_paths_socket.getSocketDlxDir(), "jre", String(version), platformArch);
	const archive = await require_external_tools_from_download.downloadAndExtractTool({
		url,
		name: `adoptium-jre-${version}-${platformArch}${require_primordials_string.StringPrototypeStartsWith(platformArch, "win-") ? ".zip" : ".tar.gz"}`,
		integrity,
		extractedDir,
		extractOptions: { strip: 1 },
		downloader
	});
	const javaBinary = node_process.default.platform === "win32" ? "java.exe" : "java";
	const macHome = node_path.default.join(extractedDir, "Contents", "Home");
	const javaHome = node_process.default.platform === "darwin" ? macHome : extractedDir;
	return {
		javaPath: node_path.default.join(javaHome, "bin", javaBinary),
		javaHome,
		source: "download",
		integrity: archive.integrity
	};
}

//#endregion
exports.jreFromDownload = jreFromDownload;