"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_primordials_error = require('../primordials/error.js');
const require_node_path = require('../node/path.js');
const require_logger_default = require('../logger/default.js');
const require_archives_detect = require('../archives/detect.js');
const require_fs_safe = require('../fs/safe.js');
const require_archives_extract = require('../archives/extract.js');
const require_releases_github_downloads = require('./github-downloads.js');

//#region src/releases/github-archives.ts
/**
* @file GitHub release archive download + extraction.
*/
const logger = require_logger_default.getDefaultLogger();
/**
* Download and extract an archive from a GitHub release. Supports zip, tar,
* tar.gz, and tgz formats. Automatically handles downloading, extracting, and
* cleanup.
*
* @example
*   ;```typescript
*   const outputDir = await downloadAndExtractArchive(
*     'v1.0.0',
*     'data-*.tar.gz',
*     '/tmp/data',
*     { owner: 'SocketDev', repo: 'socket-btm' },
*   )
*   ```
*
* @param tag - Release tag name.
* @param assetPattern - Asset name or pattern (glob string, prefix/suffix
*   object, or RegExp)
* @param outputDir - Directory to extract the archive contents to.
* @param repoConfig - Repository configuration (owner/repo)
* @param options - Additional options.
* @param options.quiet - Suppress log messages.
* @param options.cleanup - Remove downloaded archive after extraction (default:
*   true)
* @param options.strip - Strip leading path components (like tar
*   --strip-components)
* @param options.format - Archive format (auto-detected if not specified)
*
* @returns Path to the extraction directory
*/
async function downloadAndExtractArchive(tag, assetPattern, outputDir, repoConfig, options = { __proto__: null }) {
	const { cleanup = true, format, quiet = false, strip } = options;
	const path = /* @__PURE__ */ require_node_path.getNodePath();
	await require_fs_safe.safeMkdir(outputDir);
	let ext = ".archive";
	if (format) ext = format === "tar.gz" ? ".tar.gz" : `.${format}`;
	else if (typeof assetPattern === "string") {
		const detectedFormat = require_archives_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 require_releases_github_downloads.downloadReleaseAsset(tag, assetPattern, archivePath, repoConfig, { quiet });
	if (!quiet) logger.info(`Extracting archive to ${outputDir}...`);
	try {
		await require_archives_extract.extractArchive(archivePath, outputDir, {
			quiet,
			strip
		});
		if (!quiet) logger.info(`Extracted archive contents to ${outputDir}`);
	} catch (cause) {
		throw new require_primordials_error.ErrorCtor(`Failed to extract archive: ${archivePath}`, { cause });
	} finally {
		if (cleanup) try {
			await require_fs_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;
}
/**
* Download and extract a zip file from a GitHub release. Automatically handles
* downloading, extracting, and cleanup.
*
* @example
*   ;```typescript
*   const outputDir = await downloadAndExtractZip(
*     'v1.0.0',
*     'models-*.zip',
*     '/tmp/models',
*     { owner: 'SocketDev', repo: 'socket-btm' },
*   )
*   ```
*
* @param tag - Release tag name.
* @param assetPattern - Asset name or pattern (glob string, prefix/suffix
*   object, or RegExp)
* @param outputDir - Directory to extract the zip contents to.
* @param repoConfig - Repository configuration (owner/repo)
* @param options - Additional options.
* @param options.quiet - Suppress log messages.
* @param options.cleanup - Remove downloaded zip file after extraction
*   (default: true)
*
* @returns Path to the extraction directory
*/
async function downloadAndExtractZip(tag, assetPattern, outputDir, repoConfig, options = { __proto__: null }) {
	const { cleanup = true, quiet = false } = options;
	const path = /* @__PURE__ */ require_node_path.getNodePath();
	await require_fs_safe.safeMkdir(outputDir);
	const zipPath = path.join(outputDir, "__temp_download__.zip");
	if (!quiet) logger.info(`Downloading zip asset from release ${tag}...`);
	await require_releases_github_downloads.downloadReleaseAsset(tag, assetPattern, zipPath, repoConfig, { quiet });
	if (!quiet) logger.info(`Extracting zip to ${outputDir}...`);
	try {
		await require_archives_extract.extractArchive(zipPath, outputDir, { quiet });
		if (!quiet) logger.info(`Extracted zip contents to ${outputDir}`);
	} catch (cause) {
		throw new require_primordials_error.ErrorCtor(`Failed to extract zip file: ${zipPath}`, { cause });
	} finally {
		if (cleanup) try {
			await require_fs_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;
}

//#endregion
exports.downloadAndExtractArchive = downloadAndExtractArchive;
exports.downloadAndExtractZip = downloadAndExtractZip;