class B extends Error {
  constructor(t) {
    super(t), this.name = "ShikiError";
  }
}
function H() {
  return 2147483648;
}
function v() {
  return typeof performance < "u" ? performance.now() : Date.now();
}
const I = (r, t) => r + (t - r % t) % t;
async function W(r) {
  let t, n;
  const e = {};
  function l(f) {
    n = f, e.HEAPU8 = new Uint8Array(f), e.HEAPU32 = new Uint32Array(f);
  }
  function h(f, c, S) {
    e.HEAPU8.copyWithin(f, c, c + S);
  }
  function o(f) {
    try {
      return t.grow(f - n.byteLength + 65535 >>> 16), l(t.buffer), 1;
    } catch {
    }
  }
  function s(f) {
    const c = e.HEAPU8.length;
    f = f >>> 0;
    const S = H();
    if (f > S)
      return !1;
    for (let A = 1; A <= 4; A *= 2) {
      let m = c * (1 + 0.2 / A);
      m = Math.min(m, f + 100663296);
      const _ = Math.min(S, I(Math.max(f, m), 65536));
      if (o(_))
        return !0;
    }
    return !1;
  }
  const i = typeof TextDecoder < "u" ? new TextDecoder("utf8") : void 0;
  function a(f, c, S = 1024) {
    const A = c + S;
    let m = c;
    for (; f[m] && !(m >= A); ) ++m;
    if (m - c > 16 && f.buffer && i)
      return i.decode(f.subarray(c, m));
    let _ = "";
    for (; c < m; ) {
      let p = f[c++];
      if (!(p & 128)) {
        _ += String.fromCharCode(p);
        continue;
      }
      const b = f[c++] & 63;
      if ((p & 224) === 192) {
        _ += String.fromCharCode((p & 31) << 6 | b);
        continue;
      }
      const C = f[c++] & 63;
      if ((p & 240) === 224 ? p = (p & 15) << 12 | b << 6 | C : p = (p & 7) << 18 | b << 12 | C << 6 | f[c++] & 63, p < 65536)
        _ += String.fromCharCode(p);
      else {
        const E = p - 65536;
        _ += String.fromCharCode(55296 | E >> 10, 56320 | E & 1023);
      }
    }
    return _;
  }
  function w(f, c) {
    return f ? a(e.HEAPU8, f, c) : "";
  }
  const u = {
    emscripten_get_now: v,
    emscripten_memcpy_big: h,
    emscripten_resize_heap: s,
    fd_write: () => 0
  };
  async function y() {
    const c = await r({
      env: u,
      wasi_snapshot_preview1: u
    });
    t = c.memory, l(t.buffer), Object.assign(e, c), e.UTF8ToString = w;
  }
  return await y(), e;
}
var x = Object.defineProperty, V = (r, t, n) => t in r ? x(r, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : r[t] = n, g = (r, t, n) => V(r, typeof t != "symbol" ? t + "" : t, n);
let d = null;
function D(r) {
  throw new B(r.UTF8ToString(r.getLastOnigError()));
}
class T {
  constructor(t) {
    g(this, "utf16Length"), g(this, "utf8Length"), g(this, "utf16Value"), g(this, "utf8Value"), g(this, "utf16OffsetToUtf8"), g(this, "utf8OffsetToUtf16");
    const n = t.length, e = T._utf8ByteLength(t), l = e !== n, h = l ? new Uint32Array(n + 1) : null;
    l && (h[n] = e);
    const o = l ? new Uint32Array(e + 1) : null;
    l && (o[e] = n);
    const s = new Uint8Array(e);
    let i = 0;
    for (let a = 0; a < n; a++) {
      const w = t.charCodeAt(a);
      let u = w, y = !1;
      if (w >= 55296 && w <= 56319 && a + 1 < n) {
        const f = t.charCodeAt(a + 1);
        f >= 56320 && f <= 57343 && (u = (w - 55296 << 10) + 65536 | f - 56320, y = !0);
      }
      l && (h[a] = i, y && (h[a + 1] = i), u <= 127 ? o[i + 0] = a : u <= 2047 ? (o[i + 0] = a, o[i + 1] = a) : u <= 65535 ? (o[i + 0] = a, o[i + 1] = a, o[i + 2] = a) : (o[i + 0] = a, o[i + 1] = a, o[i + 2] = a, o[i + 3] = a)), u <= 127 ? s[i++] = u : u <= 2047 ? (s[i++] = 192 | (u & 1984) >>> 6, s[i++] = 128 | (u & 63) >>> 0) : u <= 65535 ? (s[i++] = 224 | (u & 61440) >>> 12, s[i++] = 128 | (u & 4032) >>> 6, s[i++] = 128 | (u & 63) >>> 0) : (s[i++] = 240 | (u & 1835008) >>> 18, s[i++] = 128 | (u & 258048) >>> 12, s[i++] = 128 | (u & 4032) >>> 6, s[i++] = 128 | (u & 63) >>> 0), y && a++;
    }
    this.utf16Length = n, this.utf8Length = e, this.utf16Value = t, this.utf8Value = s, this.utf16OffsetToUtf8 = h, this.utf8OffsetToUtf16 = o;
  }
  static _utf8ByteLength(t) {
    let n = 0;
    for (let e = 0, l = t.length; e < l; e++) {
      const h = t.charCodeAt(e);
      let o = h, s = !1;
      if (h >= 55296 && h <= 56319 && e + 1 < l) {
        const i = t.charCodeAt(e + 1);
        i >= 56320 && i <= 57343 && (o = (h - 55296 << 10) + 65536 | i - 56320, s = !0);
      }
      o <= 127 ? n += 1 : o <= 2047 ? n += 2 : o <= 65535 ? n += 3 : n += 4, s && e++;
    }
    return n;
  }
  createString(t) {
    const n = t.omalloc(this.utf8Length);
    return t.HEAPU8.set(this.utf8Value, n), n;
  }
}
const L = class U {
  constructor(t) {
    if (g(this, "id", ++U.LAST_ID), g(this, "_onigBinding"), g(this, "content"), g(this, "utf16Length"), g(this, "utf8Length"), g(this, "utf16OffsetToUtf8"), g(this, "utf8OffsetToUtf16"), g(this, "ptr"), !d)
      throw new B("Must invoke loadWasm first.");
    this._onigBinding = d, this.content = t;
    const n = new T(t);
    this.utf16Length = n.utf16Length, this.utf8Length = n.utf8Length, this.utf16OffsetToUtf8 = n.utf16OffsetToUtf8, this.utf8OffsetToUtf16 = n.utf8OffsetToUtf16, this.utf8Length < 1e4 && !U._sharedPtrInUse ? (U._sharedPtr || (U._sharedPtr = d.omalloc(1e4)), U._sharedPtrInUse = !0, d.HEAPU8.set(n.utf8Value, U._sharedPtr), this.ptr = U._sharedPtr) : this.ptr = n.createString(d);
  }
  convertUtf8OffsetToUtf16(t) {
    return this.utf8OffsetToUtf16 ? t < 0 ? 0 : t > this.utf8Length ? this.utf16Length : this.utf8OffsetToUtf16[t] : t;
  }
  convertUtf16OffsetToUtf8(t) {
    return this.utf16OffsetToUtf8 ? t < 0 ? 0 : t > this.utf16Length ? this.utf8Length : this.utf16OffsetToUtf8[t] : t;
  }
  dispose() {
    this.ptr === U._sharedPtr ? U._sharedPtrInUse = !1 : this._onigBinding.ofree(this.ptr);
  }
};
g(L, "LAST_ID", 0);
g(L, "_sharedPtr", 0);
g(L, "_sharedPtrInUse", !1);
let M = L;
class N {
  constructor(t) {
    if (g(this, "_onigBinding"), g(this, "_ptr"), !d)
      throw new B("Must invoke loadWasm first.");
    const n = [], e = [];
    for (let s = 0, i = t.length; s < i; s++) {
      const a = new T(t[s]);
      n[s] = a.createString(d), e[s] = a.utf8Length;
    }
    const l = d.omalloc(4 * t.length);
    d.HEAPU32.set(n, l / 4);
    const h = d.omalloc(4 * t.length);
    d.HEAPU32.set(e, h / 4);
    const o = d.createOnigScanner(l, h, t.length);
    for (let s = 0, i = t.length; s < i; s++)
      d.ofree(n[s]);
    d.ofree(h), d.ofree(l), o === 0 && D(d), this._onigBinding = d, this._ptr = o;
  }
  dispose() {
    this._onigBinding.freeOnigScanner(this._ptr);
  }
  findNextMatchSync(t, n, e) {
    let l = 0;
    if (typeof e == "number" && (l = e), typeof t == "string") {
      t = new M(t);
      const h = this._findNextMatchSync(t, n, !1, l);
      return t.dispose(), h;
    }
    return this._findNextMatchSync(t, n, !1, l);
  }
  _findNextMatchSync(t, n, e, l) {
    const h = this._onigBinding, o = h.findNextOnigScannerMatch(this._ptr, t.id, t.ptr, t.utf8Length, t.convertUtf16OffsetToUtf8(n), l);
    if (o === 0)
      return null;
    const s = h.HEAPU32;
    let i = o / 4;
    const a = s[i++], w = s[i++], u = [];
    for (let y = 0; y < w; y++) {
      const f = t.convertUtf8OffsetToUtf16(s[i++]), c = t.convertUtf8OffsetToUtf16(s[i++]);
      u[y] = {
        start: f,
        end: c,
        length: c - f
      };
    }
    return {
      index: a,
      captureIndices: u
    };
  }
}
function F(r) {
  return typeof r.instantiator == "function";
}
function R(r) {
  return typeof r.default == "function";
}
function k(r) {
  return typeof r.data < "u";
}
function j(r) {
  return typeof Response < "u" && r instanceof Response;
}
function z(r) {
  var t;
  return typeof ArrayBuffer < "u" && (r instanceof ArrayBuffer || ArrayBuffer.isView(r)) || typeof Buffer < "u" && ((t = Buffer.isBuffer) == null ? void 0 : t.call(Buffer, r)) || typeof SharedArrayBuffer < "u" && r instanceof SharedArrayBuffer || typeof Uint32Array < "u" && r instanceof Uint32Array;
}
let P;
function G(r) {
  if (P)
    return P;
  async function t() {
    d = await W(async (n) => {
      let e = r;
      return e = await e, typeof e == "function" && (e = await e(n)), typeof e == "function" && (e = await e(n)), F(e) ? e = await e.instantiator(n) : R(e) ? e = await e.default(n) : (k(e) && (e = e.data), j(e) ? typeof WebAssembly.instantiateStreaming == "function" ? e = await $(e)(n) : e = await J(e)(n) : z(e) ? e = await O(e)(n) : e instanceof WebAssembly.Module ? e = await O(e)(n) : "default" in e && e.default instanceof WebAssembly.Module && (e = await O(e.default)(n))), "instance" in e && (e = e.instance), "exports" in e && (e = e.exports), e;
    });
  }
  return P = t(), P;
}
function O(r) {
  return (t) => WebAssembly.instantiate(r, t);
}
function $(r) {
  return (t) => WebAssembly.instantiateStreaming(r, t);
}
function J(r) {
  return async (t) => {
    const n = await r.arrayBuffer();
    return WebAssembly.instantiate(n, t);
  };
}
async function K(r) {
  return r && await G(r), {
    createScanner(t) {
      return new N(t.map((n) => typeof n == "string" ? n : n.source));
    },
    createString(t) {
      return new M(t);
    }
  };
}
export {
  K as createOnigurumaEngine,
  G as loadWasm
};
