// LICENSE: JavaScript wrapper is provided under the MIT License
// LICENSE: WASM ZSTD decoder binary is provided under the BSD 3-Clause License, Copyright (c) 2016-2021, Yann Collet, Facebook, Inc. All rights reserved.


// Derived from https://github.com/donmccurdy/zstddec with modifications for compatibility by wiedehopf (matthias.wirth@gmail.com)
// See https://github.com/wiedehopf/zstddec-tar1090 for more info
// For browser compatibility, will return UINT32_MAX / 0xffffffff on error in findDecompressedSize, decompressed filesize limited to 4 GB

"use strict";

!function(A, I) {
  "object" == typeof exports && "undefined" != typeof module ? I(exports) : "function" == typeof define && define.amd ? define(["exports"], I) : I((A = "undefined" != typeof globalThis ? globalThis : A || self).zstddec = {})
}(this, (function(A) {
  "use strict";
  let I, B, g;
  const Q = {
    env: {
      emscripten_notify_memory_growth: function(A) {
        g = new Uint8Array(B.exports.memory.buffer)
        console.log("zstddec: wasm memory size increased to: " + g.byteLength);
      }
    }
  };
  A.ZSTDDecoder = class {
    init() {
      if (I) { return I; };
      if ("undefined" != typeof fetch) {
        I = fetch("data:application/wasm;base64," + C);
        if (WebAssembly.instantiateStreaming) {
          I = WebAssembly.instantiateStreaming(I, Q);
        } else {
          I = I.then((A=>A.arrayBuffer()))
            .then((A=>WebAssembly.instantiate(A, Q)));
        }
      } else {
        I = WebAssembly.instantiate(Buffer.from(C, "base64"), Q);
      }
      I = I.then(this._init)
        .catch((e) => { try { webAssemblyFail(e); } catch (e) { console.error(e); } });
      return I;
    }
    _init(A) {
      C = null; // free a bit of memory
      B = A.instance;
      Q.env.emscripten_notify_memory_growth(0);
    }
    // returns: Uint8Array
    // arguments: Uint8Array with compressed data, optionally the uncompressed size
    decode(A, I=0) {
      if (!B) { throw new Error("ZSTDDecoder: Await .init() before decoding."); };
      if (A == null || A.buffer == null || A.byteLength == null) { throw new Error("ZSTDDecoder: first argument needs to be an Uint8Array with compressed data"); };
      const Q = A.byteLength;
      const C = B.exports.malloc(Q);
      g.set(A, C);
      I = I || B.exports.ZSTD_findDecompressedSize(C, Q);
      if (I == 0xffffffff) {
        B.exports.free(C);
        throw new Error("ZSTDDecoder: ZSTD_findDecompressedSize failed, possible reasons: uncompressed size >= 4294967295 bytes; an error occurred (e.g. invalid magic number, srcSize too small)");
      }
      const E = B.exports.malloc(I);
      const res = B.exports.ZSTD_decompress(E, I, C, Q);
      if (B.exports.ZSTD_isError(res)) {
        B.exports.free(C); B.exports.free(E);
        throw new Error("ZSTDDecoder: decompression error: " + res);
      }
      let D = g.slice(E, E + res);
      B.exports.free(C); B.exports.free(E);
      return D;
    }
  }
    ,
    Object.defineProperty(A, "__esModule", {
      value: !0
    });
  let C = "[encoded data, 64424 characters]"
}
));
