"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var brotli_exports = {};
__export(brotli_exports, {
  BROTLI_EXTS: () => BROTLI_EXTS,
  compressBrotli: () => compressBrotli,
  compressBrotliFile: () => compressBrotliFile,
  createBrotliCompressor: () => createBrotliCompressor,
  createBrotliDecompressor: () => createBrotliDecompressor,
  decompressBrotli: () => decompressBrotli,
  decompressBrotliFile: () => decompressBrotliFile,
  hasBrotliExt: () => hasBrotliExt,
  isBrotliCompressed: () => isBrotliCompressed,
  resolveBrotliOptions: () => resolveBrotliOptions
});
module.exports = __toCommonJS(brotli_exports);
var import_node_buffer = require("node:buffer");
var import_node_fs = require("node:fs");
var import_node_path = __toESM(require("node:path"));
var import_promises = require("node:stream/promises");
var import_node_util = require("node:util");
var import_node_zlib = require("node:zlib");
var import_safe = require("../fs/safe");
var import_error = require("../primordials/error");
var import_string = require("../primordials/string");
var import_internal = require("./_internal");
const brotliCompressAsync = (0, import_node_util.promisify)(import_node_zlib.brotliCompress);
const brotliDecompressAsync = (0, import_node_util.promisify)(import_node_zlib.brotliDecompress);
function resolveBrotliOptions(options) {
  const level = options?.level ?? 11;
  const params = {
    [import_node_zlib.constants.BROTLI_PARAM_QUALITY]: level
  };
  if (options?.size !== void 0 && options.size > 0) {
    params[import_node_zlib.constants.BROTLI_PARAM_SIZE_HINT] = options.size;
  }
  return { params };
}
async function compressBrotli(input, options) {
  const buf = typeof input === "string" ? import_node_buffer.Buffer.from(input, "utf8") : input;
  const opts = resolveBrotliOptions(options);
  if (opts.params[import_node_zlib.constants.BROTLI_PARAM_SIZE_HINT] === void 0) {
    opts.params[import_node_zlib.constants.BROTLI_PARAM_SIZE_HINT] = buf.byteLength;
  }
  return await brotliCompressAsync(buf, opts);
}
async function decompressBrotli(input) {
  return await brotliDecompressAsync(input);
}
async function compressBrotliFile(srcPath, destOrOptions, maybeOptions) {
  const { destPath, options, inPlace } = (0, import_internal.resolveFileArgs)(
    "compressBrotliFile",
    srcPath,
    destOrOptions,
    maybeOptions,
    (p) => `${p}.br`
  );
  await (0, import_promises.pipeline)(
    (0, import_node_fs.createReadStream)(srcPath),
    (0, import_node_zlib.createBrotliCompress)(resolveBrotliOptions(options)),
    (0, import_node_fs.createWriteStream)(destPath)
  );
  if (inPlace) {
    await (0, import_safe.safeDelete)(srcPath);
  }
  return destPath;
}
async function decompressBrotliFile(srcPath, destOrOptions) {
  const { destPath, inPlace } = (0, import_internal.resolveFileArgs)(
    "decompressBrotliFile",
    srcPath,
    destOrOptions,
    void 0,
    (p) => {
      if (!hasBrotliExt(p)) {
        throw new import_error.ErrorCtor(
          `decompressBrotliFile: ${p} has no .br/.brotli extension; can't infer destination`
        );
      }
      return (0, import_internal.stripExt)(p, BROTLI_EXTS);
    }
  );
  await (0, import_promises.pipeline)(
    (0, import_node_fs.createReadStream)(srcPath),
    (0, import_node_zlib.createBrotliDecompress)(),
    (0, import_node_fs.createWriteStream)(destPath)
  );
  if (inPlace) {
    await (0, import_safe.safeDelete)(srcPath);
  }
  return destPath;
}
function createBrotliCompressor(options) {
  return (0, import_node_zlib.createBrotliCompress)(resolveBrotliOptions(options));
}
function createBrotliDecompressor() {
  return (0, import_node_zlib.createBrotliDecompress)();
}
const BROTLI_MIN_LEN = 4;
function isBrotliCompressed(input) {
  return import_node_buffer.Buffer.isBuffer(input) && input.byteLength >= BROTLI_MIN_LEN;
}
const BROTLI_EXTS = /* @__PURE__ */ new Set([".br", ".brotli"]);
function hasBrotliExt(filePath) {
  return BROTLI_EXTS.has((0, import_string.StringPrototypeToLowerCase)(import_node_path.default.extname(filePath)));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  BROTLI_EXTS,
  compressBrotli,
  compressBrotliFile,
  createBrotliCompressor,
  createBrotliDecompressor,
  decompressBrotli,
  decompressBrotliFile,
  hasBrotliExt,
  isBrotliCompressed,
  resolveBrotliOptions
});
