import { commonjsGlobal as F } from "../../../../../../_virtual/_commonjsHelpers.js";
import { __exports as I } from "../../../../../../_virtual/timsort.js";
(function(l) {
  (function(A, G) {
    G(l);
  })(F, function(A) {
    A.__esModule = !0, A.sort = D;
    function G(i, u) {
      if (!(i instanceof u))
        throw new TypeError("Cannot call a class as a function");
    }
    var E = 32, R = 7, d = 256, T = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9];
    function z(i) {
      return i < 1e5 ? i < 100 ? i < 10 ? 0 : 1 : i < 1e4 ? i < 1e3 ? 2 : 3 : 4 : i < 1e7 ? i < 1e6 ? 5 : 6 : i < 1e9 ? i < 1e8 ? 7 : 8 : 9;
    }
    function M(i, u) {
      if (i === u)
        return 0;
      if (~~i === i && ~~u === u) {
        if (i === 0 || u === 0)
          return i < u ? -1 : 1;
        if (i < 0 || u < 0) {
          if (u >= 0)
            return -1;
          if (i >= 0)
            return 1;
          i = -i, u = -u;
        }
        var s = z(i), t = z(u), a = 0;
        return s < t ? (i *= T[t - s - 1], u /= 10, a = -1) : s > t && (u *= T[s - t - 1], i /= 10, a = 1), i === u ? a : i < u ? -1 : 1;
      }
      var n = String(i), h = String(u);
      return n === h ? 0 : n < h ? -1 : 1;
    }
    function C(i) {
      for (var u = 0; i >= E; )
        u |= i & 1, i >>= 1;
      return i + u;
    }
    function O(i, u, s, t) {
      var a = u + 1;
      if (a === s)
        return 1;
      if (t(i[a++], i[u]) < 0) {
        for (; a < s && t(i[a], i[a - 1]) < 0; )
          a++;
        H(i, u, a);
      } else
        for (; a < s && t(i[a], i[a - 1]) >= 0; )
          a++;
      return a - u;
    }
    function H(i, u, s) {
      for (s--; u < s; ) {
        var t = i[u];
        i[u++] = i[s], i[s--] = t;
      }
    }
    function b(i, u, s, t, a) {
      for (t === u && t++; t < s; t++) {
        for (var n = i[t], h = u, f = t; h < f; ) {
          var r = h + f >>> 1;
          a(n, i[r]) < 0 ? f = r : h = r + 1;
        }
        var e = t - h;
        switch (e) {
          case 3:
            i[h + 3] = i[h + 2];
          case 2:
            i[h + 2] = i[h + 1];
          case 1:
            i[h + 1] = i[h];
            break;
          default:
            for (; e > 0; )
              i[h + e] = i[h + e - 1], e--;
        }
        i[h] = n;
      }
    }
    function c(i, u, s, t, a, n) {
      var h = 0, f = 0, r = 1;
      if (n(i, u[s + a]) > 0) {
        for (f = t - a; r < f && n(i, u[s + a + r]) > 0; )
          h = r, r = (r << 1) + 1, r <= 0 && (r = f);
        r > f && (r = f), h += a, r += a;
      } else {
        for (f = a + 1; r < f && n(i, u[s + a - r]) <= 0; )
          h = r, r = (r << 1) + 1, r <= 0 && (r = f);
        r > f && (r = f);
        var e = h;
        h = a - r, r = a - e;
      }
      for (h++; h < r; ) {
        var o = h + (r - h >>> 1);
        n(i, u[s + o]) > 0 ? h = o + 1 : r = o;
      }
      return r;
    }
    function _(i, u, s, t, a, n) {
      var h = 0, f = 0, r = 1;
      if (n(i, u[s + a]) < 0) {
        for (f = a + 1; r < f && n(i, u[s + a - r]) < 0; )
          h = r, r = (r << 1) + 1, r <= 0 && (r = f);
        r > f && (r = f);
        var e = h;
        h = a - r, r = a - e;
      } else {
        for (f = t - a; r < f && n(i, u[s + a + r]) >= 0; )
          h = r, r = (r << 1) + 1, r <= 0 && (r = f);
        r > f && (r = f), h += a, r += a;
      }
      for (h++; h < r; ) {
        var o = h + (r - h >>> 1);
        n(i, u[s + o]) < 0 ? r = o : h = o + 1;
      }
      return r;
    }
    var N = function() {
      function i(u, s) {
        G(this, i), this.array = null, this.compare = null, this.minGallop = R, this.length = 0, this.tmpStorageLength = d, this.stackLength = 0, this.runStart = null, this.runLength = null, this.stackSize = 0, this.array = u, this.compare = s, this.length = u.length, this.length < 2 * d && (this.tmpStorageLength = this.length >>> 1), this.tmp = new Array(this.tmpStorageLength), this.stackLength = this.length < 120 ? 5 : this.length < 1542 ? 10 : this.length < 119151 ? 19 : 40, this.runStart = new Array(this.stackLength), this.runLength = new Array(this.stackLength);
      }
      return i.prototype.pushRun = function(s, t) {
        this.runStart[this.stackSize] = s, this.runLength[this.stackSize] = t, this.stackSize += 1;
      }, i.prototype.mergeRuns = function() {
        for (; this.stackSize > 1; ) {
          var s = this.stackSize - 2;
          if (s >= 1 && this.runLength[s - 1] <= this.runLength[s] + this.runLength[s + 1] || s >= 2 && this.runLength[s - 2] <= this.runLength[s] + this.runLength[s - 1])
            this.runLength[s - 1] < this.runLength[s + 1] && s--;
          else if (this.runLength[s] > this.runLength[s + 1])
            break;
          this.mergeAt(s);
        }
      }, i.prototype.forceMergeRuns = function() {
        for (; this.stackSize > 1; ) {
          var s = this.stackSize - 2;
          s > 0 && this.runLength[s - 1] < this.runLength[s + 1] && s--, this.mergeAt(s);
        }
      }, i.prototype.mergeAt = function(s) {
        var t = this.compare, a = this.array, n = this.runStart[s], h = this.runLength[s], f = this.runStart[s + 1], r = this.runLength[s + 1];
        this.runLength[s] = h + r, s === this.stackSize - 3 && (this.runStart[s + 1] = this.runStart[s + 2], this.runLength[s + 1] = this.runLength[s + 2]), this.stackSize--;
        var e = _(a[f], a, n, h, 0, t);
        n += e, h -= e, h !== 0 && (r = c(a[n + h - 1], a, f, r, r - 1, t), r !== 0 && (h <= r ? this.mergeLow(n, h, f, r) : this.mergeHigh(n, h, f, r)));
      }, i.prototype.mergeLow = function(s, t, a, n) {
        var h = this.compare, f = this.array, r = this.tmp, e = 0;
        for (e = 0; e < t; e++)
          r[e] = f[s + e];
        var o = 0, L = a, v = s;
        if (f[v++] = f[L++], --n === 0) {
          for (e = 0; e < t; e++)
            f[v + e] = r[o + e];
          return;
        }
        if (t === 1) {
          for (e = 0; e < n; e++)
            f[v + e] = f[L + e];
          f[v + n] = r[o];
          return;
        }
        for (var w = this.minGallop; ; ) {
          var k = 0, p = 0, m = !1;
          do
            if (h(f[L], r[o]) < 0) {
              if (f[v++] = f[L++], p++, k = 0, --n === 0) {
                m = !0;
                break;
              }
            } else if (f[v++] = r[o++], k++, p = 0, --t === 1) {
              m = !0;
              break;
            }
          while ((k | p) < w);
          if (m)
            break;
          do {
            if (k = _(f[L], r, o, t, 0, h), k !== 0) {
              for (e = 0; e < k; e++)
                f[v + e] = r[o + e];
              if (v += k, o += k, t -= k, t <= 1) {
                m = !0;
                break;
              }
            }
            if (f[v++] = f[L++], --n === 0) {
              m = !0;
              break;
            }
            if (p = c(r[o], f, L, n, 0, h), p !== 0) {
              for (e = 0; e < p; e++)
                f[v + e] = f[L + e];
              if (v += p, L += p, n -= p, n === 0) {
                m = !0;
                break;
              }
            }
            if (f[v++] = r[o++], --t === 1) {
              m = !0;
              break;
            }
            w--;
          } while (k >= R || p >= R);
          if (m)
            break;
          w < 0 && (w = 0), w += 2;
        }
        if (this.minGallop = w, w < 1 && (this.minGallop = 1), t === 1) {
          for (e = 0; e < n; e++)
            f[v + e] = f[L + e];
          f[v + n] = r[o];
        } else {
          if (t === 0)
            throw new Error("mergeLow preconditions were not respected");
          for (e = 0; e < t; e++)
            f[v + e] = r[o + e];
        }
      }, i.prototype.mergeHigh = function(s, t, a, n) {
        var h = this.compare, f = this.array, r = this.tmp, e = 0;
        for (e = 0; e < n; e++)
          r[e] = f[a + e];
        var o = s + t - 1, L = n - 1, v = a + n - 1, w = 0, k = 0;
        if (f[v--] = f[o--], --t === 0) {
          for (w = v - (n - 1), e = 0; e < n; e++)
            f[w + e] = r[e];
          return;
        }
        if (n === 1) {
          for (v -= t, o -= t, k = v + 1, w = o + 1, e = t - 1; e >= 0; e--)
            f[k + e] = f[w + e];
          f[v] = r[L];
          return;
        }
        for (var p = this.minGallop; ; ) {
          var m = 0, S = 0, g = !1;
          do
            if (h(r[L], f[o]) < 0) {
              if (f[v--] = f[o--], m++, S = 0, --t === 0) {
                g = !0;
                break;
              }
            } else if (f[v--] = r[L--], S++, m = 0, --n === 1) {
              g = !0;
              break;
            }
          while ((m | S) < p);
          if (g)
            break;
          do {
            if (m = t - _(r[L], f, s, t, t - 1, h), m !== 0) {
              for (v -= m, o -= m, t -= m, k = v + 1, w = o + 1, e = m - 1; e >= 0; e--)
                f[k + e] = f[w + e];
              if (t === 0) {
                g = !0;
                break;
              }
            }
            if (f[v--] = r[L--], --n === 1) {
              g = !0;
              break;
            }
            if (S = n - c(f[o], r, 0, n, n - 1, h), S !== 0) {
              for (v -= S, L -= S, n -= S, k = v + 1, w = L + 1, e = 0; e < S; e++)
                f[k + e] = r[w + e];
              if (n <= 1) {
                g = !0;
                break;
              }
            }
            if (f[v--] = f[o--], --t === 0) {
              g = !0;
              break;
            }
            p--;
          } while (m >= R || S >= R);
          if (g)
            break;
          p < 0 && (p = 0), p += 2;
        }
        if (this.minGallop = p, p < 1 && (this.minGallop = 1), n === 1) {
          for (v -= t, o -= t, k = v + 1, w = o + 1, e = t - 1; e >= 0; e--)
            f[k + e] = f[w + e];
          f[v] = r[L];
        } else {
          if (n === 0)
            throw new Error("mergeHigh preconditions were not respected");
          for (w = v - (n - 1), e = 0; e < n; e++)
            f[w + e] = r[e];
        }
      }, i;
    }();
    function D(i, u, s, t) {
      if (!Array.isArray(i))
        throw new TypeError("Can only sort arrays");
      u ? typeof u != "function" && (t = s, s = u, u = M) : u = M, s || (s = 0), t || (t = i.length);
      var a = t - s;
      if (!(a < 2)) {
        var n = 0;
        if (a < E) {
          n = O(i, s, t, u), b(i, s, t, s + n, u);
          return;
        }
        var h = new N(i, u), f = C(a);
        do {
          if (n = O(i, s, t, u), n < f) {
            var r = a;
            r > f && (r = f), b(i, s, s + r, s + n, u), n = r;
          }
          h.pushRun(s, n), h.mergeRuns(), a -= n, s += n;
        } while (a !== 0);
        h.forceMergeRuns();
      }
    }
  });
})(I);
//# sourceMappingURL=timsort.js.map
