var __defProp = Object.defineProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
  __markAsModule(target);
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
__export(exports, {
  compress: () => compress,
  compress_stream: () => compress_stream,
  compress_stream_sync: () => compress_stream_sync,
  decompress: () => decompress,
  decompress_with: () => decompress_with
});
let u8;
let wasm;
console.log("zstd module is deprecated");
{
  const module2 = new WebAssembly.Module(require('fs').readFileSync(require('path').join(__dirname, '../../src/zstd/zstd.wasm')));
  const instance = new WebAssembly.Instance(module2, {
    env: { emscripten_notify_memory_growth() {
      u8 = new Uint8Array(wasm.memory.buffer);
    } }
  });
  wasm = instance.exports;
  u8 = new Uint8Array(wasm.memory.buffer);
}
function load_static_string(u82, ptr) {
  let s = "";
  const l = u82.length | 0;
  for (let o = ptr | 0; o < l; o++) {
    const x = u82[o];
    if (x === 0)
      break;
    s += String.fromCharCode(x);
  }
  return s;
}
function view(buffer, shared = false) {
  if (buffer instanceof ArrayBuffer)
    return new Uint8Array(buffer);
  if (shared && buffer instanceof SharedArrayBuffer)
    return new Uint8Array(buffer);
  if (ArrayBuffer.isView(buffer))
    return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
}
function from_parts(buffers, shared = false) {
  let length = 0;
  let offset = 0;
  buffers.forEach((buffer) => length += buffer.byteLength == null ? buffer.length : buffer.byteLength);
  const u82 = new Uint8Array(shared ? new SharedArrayBuffer(length) : length);
  buffers.forEach((buffer) => {
    const ref = Array.isArray(buffer) ? buffer : view(buffer, true);
    u82.set(ref, offset);
    offset += ref.length;
  });
  return u82;
}
function compress(buffer, { level = 3, checksum = false } = {}) {
  const slices = [];
  for (const chunk of compress_stream_sync([buffer], { level, checksum, size: buffer.length }))
    slices.push(chunk);
  return from_parts(slices);
}
function decompress(buffer, size) {
  const b = wasm.malloc(buffer.length);
  u8.set(buffer, b);
  if (size == null) {
    size = Number(wasm.ZSTD_findDecompressedSize(b, buffer.length));
    if (size === -2)
      throw wasm.free(b), new Error("zstd: not zstd");
    if (size === -1)
      throw wasm.free(b), new Error("zstd: can't find decompressed size");
  }
  const d = wasm.malloc(size);
  size = wasm.ZSTD_decompress(d, size, b, buffer.length);
  if (wasm.ZSTD_isError(size))
    throw wasm.free(b), wasm.free(d), new Error(`zstd: ${load_static_string(u8, wasm.ZSTD_getErrorName(size))}`);
  const slice = u8.slice(d, d + size);
  return wasm.free(b), wasm.free(d), slice;
}
function decompress_with(buffer, size, transform) {
  const b = wasm.malloc(buffer.length);
  u8.set(buffer, b);
  if (size == null) {
    size = Number(wasm.ZSTD_findDecompressedSize(b, buffer.length));
    if (size === -2)
      throw wasm.free(b), new Error("zstd: not zstd");
    if (size === -1)
      throw wasm.free(b), new Error("zstd: can't find decompressed size");
  }
  const d = wasm.malloc(size);
  size = wasm.ZSTD_decompress(d, size, b, buffer.length);
  if (wasm.ZSTD_isError(size))
    throw wasm.free(b), wasm.free(d), new Error(`zstd: ${load_static_string(u8, wasm.ZSTD_getErrorName(size))}`);
  const value = transform(u8.subarray(d, d + size));
  return wasm.free(b), wasm.free(d), value;
}
async function* compress_stream(iter, { size, level = 3, checksum = false } = {}) {
  const ctx = wasm.ZSTD_createCCtx();
  const bs = wasm.ZSTD_CStreamInSize();
  const cs = wasm.ZSTD_CStreamOutSize();
  const b = wasm.malloc(bs);
  const c = wasm.malloc(cs);
  if (size != null)
    wasm.ZSTD_CCtx_setPledgedSrcSize(ctx, BigInt(size));
  {
    const status = wasm.ZSTD_CCtx_setParameter(ctx, 100, level);
    if (wasm.ZSTD_isError(status))
      throw new Error(`zstd: ${load_static_string(u8, wasm.ZSTD_getErrorName(status))}`);
  }
  if (checksum) {
    const status = wasm.ZSTD_CCtx_setParameter(ctx, 201, 1);
    if (wasm.ZSTD_isError(status))
      throw new Error(`zstd: ${load_static_string(u8, wasm.ZSTD_getErrorName(status))}`);
  }
  try {
    for await (const chunk of iter) {
      let offset = 0 | 0;
      const l = chunk.length | 0;
      while (l > offset) {
        const s = chunk.subarray(offset, offset += bs);
        u8.set(s, b);
        const i2 = wasm.ZSTD_compressStream2_wasm_input(s.length, b);
        while (true) {
          const status = wasm.ZSTD_compressStream2_wasm_write(0, cs, c, ctx, i2);
          if (wasm.ZSTD_isError(status))
            throw wasm.free(i2), new Error(`zstd: ${load_static_string(u8, wasm.ZSTD_getErrorName(status))}`);
          const pos = wasm.ZSTD_compressStream2_wasm_p();
          if (pos !== 0)
            yield u8.slice(c, c + pos);
          if (status === 1)
            break;
        }
        wasm.free(i2);
      }
    }
    const i = wasm.ZSTD_compressStream2_wasm_input(0, b);
    while (true) {
      const status = wasm.ZSTD_compressStream2_wasm_write(1, cs, c, ctx, i);
      if (wasm.ZSTD_isError(status))
        throw wasm.free(i), new Error(`zstd: ${load_static_string(u8, wasm.ZSTD_getErrorName(status))}`);
      const pos = wasm.ZSTD_compressStream2_wasm_p();
      if (pos !== 0)
        yield u8.slice(c, c + pos);
      if (status === 1)
        break;
    }
    wasm.free(i);
  } finally {
    wasm.free(b);
    wasm.free(c);
    wasm.ZSTD_freeCCtx(ctx);
  }
}
function* compress_stream_sync(iter, { size, level = 3, checksum = false } = {}) {
  const ctx = wasm.ZSTD_createCCtx();
  const bs = wasm.ZSTD_CStreamInSize();
  const cs = wasm.ZSTD_CStreamOutSize();
  const b = wasm.malloc(bs);
  const c = wasm.malloc(cs);
  if (size != null)
    wasm.ZSTD_CCtx_setPledgedSrcSize(ctx, BigInt(size));
  {
    const status = wasm.ZSTD_CCtx_setParameter(ctx, 100, level);
    if (wasm.ZSTD_isError(status))
      throw new Error(`zstd: ${load_static_string(u8, wasm.ZSTD_getErrorName(status))}`);
  }
  if (checksum) {
    const status = wasm.ZSTD_CCtx_setParameter(ctx, 201, 1);
    if (wasm.ZSTD_isError(status))
      throw new Error(`zstd: ${load_static_string(u8, wasm.ZSTD_getErrorName(status))}`);
  }
  try {
    for (const chunk of iter) {
      let offset = 0 | 0;
      const l = chunk.length | 0;
      while (l > offset) {
        const s = chunk.subarray(offset, offset += bs);
        u8.set(s, b);
        const i2 = wasm.ZSTD_compressStream2_wasm_input(s.length, b);
        while (true) {
          const status = wasm.ZSTD_compressStream2_wasm_write(0, cs, c, ctx, i2);
          if (wasm.ZSTD_isError(status))
            throw wasm.free(i2), new Error(`zstd: ${load_static_string(u8, wasm.ZSTD_getErrorName(status))}`);
          const pos = wasm.ZSTD_compressStream2_wasm_p();
          if (pos !== 0)
            yield u8.slice(c, c + pos);
          if (status === 1)
            break;
        }
        wasm.free(i2);
      }
    }
    const i = wasm.ZSTD_compressStream2_wasm_input(0, b);
    while (true) {
      const status = wasm.ZSTD_compressStream2_wasm_write(1, cs, c, ctx, i);
      if (wasm.ZSTD_isError(status))
        throw wasm.free(i), new Error(`zstd: ${load_static_string(u8, wasm.ZSTD_getErrorName(status))}`);
      const pos = wasm.ZSTD_compressStream2_wasm_p();
      if (pos !== 0)
        yield u8.slice(c, c + pos);
      if (status === 1)
        break;
    }
    wasm.free(i);
  } finally {
    wasm.free(b);
    wasm.free(c);
    wasm.ZSTD_freeCCtx(ctx);
  }
}
