"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 zip_exports = {};
__export(zip_exports, {
  extractZip: () => extractZip
});
module.exports = __toCommonJS(zip_exports);
var import_safe = require("../fs/safe");
var import_normalize = require("../paths/normalize");
var import_array = require("../primordials/array");
var import_error = require("../primordials/error");
var import_map_set = require("../primordials/map-set");
var import_promise = require("../primordials/promise");
var import_internal = require("./_internal");
async function extractZip(archivePath, outputDir, options = {}) {
  (0, import_internal.assertArchiveExists)(archivePath);
  const {
    maxEntries = import_internal.DEFAULT_MAX_ENTRIES,
    maxFileSize = import_internal.DEFAULT_MAX_FILE_SIZE,
    maxTotalSize = import_internal.DEFAULT_MAX_TOTAL_SIZE,
    strip = 0
  } = options;
  const normalizedOutputDir = (0, import_normalize.normalizePath)(outputDir);
  await (0, import_safe.safeMkdir)(normalizedOutputDir);
  const AdmZip = (0, import_internal.getAdmZip)();
  const zip = new AdmZip(archivePath);
  const path = (0, import_internal.getPath)();
  const entries = zip.getEntries();
  if (entries.length > maxEntries) {
    throw new import_error.ErrorCtor(
      `Archive has too many entries: ${entries.length} (limit: ${maxEntries})`
    );
  }
  let totalExtractedSize = 0;
  for (const entry of entries) {
    if (entry.isDirectory) {
      continue;
    }
    if (entry.entryName.includes("\0")) {
      throw new import_error.ErrorCtor(
        `Invalid null byte in archive entry name: ${entry.entryName}`
      );
    }
    const uncompressedSize = entry.header.size;
    if (uncompressedSize > maxFileSize) {
      throw new import_error.ErrorCtor(
        `File size exceeds limit: ${entry.entryName} (${uncompressedSize} bytes > ${maxFileSize} bytes)`
      );
    }
    totalExtractedSize += uncompressedSize;
    if (totalExtractedSize > maxTotalSize) {
      throw new import_error.ErrorCtor(
        `Total extracted size exceeds limit: ${totalExtractedSize} bytes > ${maxTotalSize} bytes`
      );
    }
    const parts = entry.entryName.split("/");
    if (parts.length <= strip) {
      continue;
    }
    const strippedPath = (0, import_array.ArrayPrototypeSlice)(parts, strip).join("/");
    const targetPath = path.join(normalizedOutputDir, strippedPath);
    (0, import_internal.validatePathWithinBase)(targetPath, normalizedOutputDir, entry.entryName);
  }
  if (strip === 0) {
    for (const entry of entries) {
      if (!entry.isDirectory) {
        const targetPath = path.join(normalizedOutputDir, entry.entryName);
        (0, import_internal.validatePathWithinBase)(targetPath, normalizedOutputDir, entry.entryName);
      }
    }
    zip.extractAllTo(normalizedOutputDir, true);
  } else {
    const path2 = (0, import_internal.getPath)();
    const entries2 = zip.getEntries();
    const dirsToCreate = new import_map_set.SetCtor();
    for (const entry of entries2) {
      if (entry.isDirectory) {
        continue;
      }
      const parts = entry.entryName.split("/");
      if (parts.length <= strip) {
        continue;
      }
      const strippedPath = (0, import_array.ArrayPrototypeSlice)(parts, strip).join("/");
      const targetPath = path2.join(normalizedOutputDir, strippedPath);
      dirsToCreate.add(path2.dirname(targetPath));
    }
    await (0, import_promise.PromiseAll)((0, import_array.ArrayFrom)(dirsToCreate).map((dir) => (0, import_safe.safeMkdir)(dir)));
    for (const entry of entries2) {
      if (entry.isDirectory) {
        continue;
      }
      const parts = entry.entryName.split("/");
      if (parts.length <= strip) {
        continue;
      }
      const strippedPath = (0, import_array.ArrayPrototypeSlice)(parts, strip).join("/");
      const targetPath = path2.join(normalizedOutputDir, strippedPath);
      zip.extractEntryTo(entry, path2.dirname(targetPath), false, true);
    }
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  extractZip
});
