"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 github_archives_exports = {};
__export(github_archives_exports, {
  downloadAndExtractArchive: () => downloadAndExtractArchive,
  downloadAndExtractZip: () => downloadAndExtractZip
});
module.exports = __toCommonJS(github_archives_exports);
var import_detect = require("../archives/detect");
var import_extract = require("../archives/extract");
var import_safe = require("../fs/safe");
var import_logger = require("../logger/logger");
var import_error = require("../primordials/error");
var import_github_downloads = require("./github-downloads");
var import_path = require("../node/path");
const logger = (0, import_logger.getDefaultLogger)();
async function downloadAndExtractArchive(tag, assetPattern, outputDir, repoConfig, options = { __proto__: null }) {
  const { cleanup = true, format, quiet = false, strip } = options;
  const path = (0, import_path.getNodePath)();
  await (0, import_safe.safeMkdir)(outputDir);
  let ext = ".archive";
  if (format) {
    ext = format === "tar.gz" ? ".tar.gz" : `.${format}`;
  } else if (typeof assetPattern === "string") {
    const detectedFormat = (0, import_detect.detectArchiveFormat)(assetPattern);
    if (detectedFormat) {
      ext = detectedFormat === "tar.gz" ? ".tar.gz" : `.${detectedFormat}`;
    }
  }
  const archivePath = path.join(outputDir, `__temp_download__${ext}`);
  if (!quiet) {
    logger.info(`Downloading archive from release ${tag}...`);
  }
  await (0, import_github_downloads.downloadReleaseAsset)(tag, assetPattern, archivePath, repoConfig, {
    quiet
  });
  if (!quiet) {
    logger.info(`Extracting archive to ${outputDir}...`);
  }
  try {
    await (0, import_extract.extractArchive)(archivePath, outputDir, { quiet, strip });
    if (!quiet) {
      logger.info(`Extracted archive contents to ${outputDir}`);
    }
  } catch (cause) {
    throw new import_error.ErrorCtor(`Failed to extract archive: ${archivePath}`, { cause });
  } finally {
    if (cleanup) {
      try {
        await (0, import_safe.safeDelete)(archivePath);
        if (!quiet) {
          logger.info("Cleaned up temporary archive file");
        }
      } catch (e) {
        if (!quiet) {
          logger.warn(`Failed to cleanup archive file: ${e}`);
        }
      }
    }
  }
  return outputDir;
}
async function downloadAndExtractZip(tag, assetPattern, outputDir, repoConfig, options = {
  __proto__: null
}) {
  const { cleanup = true, quiet = false } = options;
  const path = (0, import_path.getNodePath)();
  await (0, import_safe.safeMkdir)(outputDir);
  const zipPath = path.join(outputDir, "__temp_download__.zip");
  if (!quiet) {
    logger.info(`Downloading zip asset from release ${tag}...`);
  }
  await (0, import_github_downloads.downloadReleaseAsset)(tag, assetPattern, zipPath, repoConfig, { quiet });
  if (!quiet) {
    logger.info(`Extracting zip to ${outputDir}...`);
  }
  try {
    await (0, import_extract.extractArchive)(zipPath, outputDir, { quiet });
    if (!quiet) {
      logger.info(`Extracted zip contents to ${outputDir}`);
    }
  } catch (cause) {
    throw new import_error.ErrorCtor(`Failed to extract zip file: ${zipPath}`, { cause });
  } finally {
    if (cleanup) {
      try {
        await (0, import_safe.safeDelete)(zipPath);
        if (!quiet) {
          logger.info("Cleaned up temporary zip file");
        }
      } catch (e) {
        if (!quiet) {
          logger.warn(`Failed to cleanup zip file: ${e}`);
        }
      }
    }
  }
  return outputDir;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  downloadAndExtractArchive,
  downloadAndExtractZip
});
