"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var from_download_exports = {};
__export(from_download_exports, {
  downloadAndExtractTool: () => downloadAndExtractTool,
  downloadToolArchive: () => downloadToolArchive
});
module.exports = __toCommonJS(from_download_exports);
var import_node_fs = require("node:fs");
var import_node_fs2 = require("node:fs");
var import_extract = require("../archives/extract");
var import_safe = require("../fs/safe");
var import_binary_download = require("../dlx/binary-download");
async function downloadAndExtractTool(opts) {
  const archive = await downloadToolArchive(opts);
  const { extractOptions, extractedDir } = opts;
  let extracted = false;
  if ((0, import_node_fs.existsSync)(extractedDir)) {
    const entries = await import_node_fs2.promises.readdir(extractedDir);
    if (entries.length === 0) {
      await (0, import_extract.extractArchive)(archive.archivePath, extractedDir, extractOptions);
      extracted = true;
    }
  } else {
    await (0, import_safe.safeMkdir)(extractedDir);
    await (0, import_extract.extractArchive)(archive.archivePath, extractedDir, extractOptions);
    extracted = true;
  }
  return {
    ...archive,
    extractedDir,
    extracted
  };
}
async function downloadToolArchive(opts) {
  const { downloader, integrity, name, url } = opts;
  const download = downloader ?? import_binary_download.downloadBinary;
  const result = await download({
    url,
    name,
    hash: integrity
  });
  return {
    archivePath: result.binaryPath,
    downloaded: result.downloaded,
    integrity: result.integrity,
    source: "download"
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  downloadAndExtractTool,
  downloadToolArchive
});
