function J(e, t, o) {
  const n = k(e, t, o), s = n < 0 ? -(n + 1) : n;
  e.splice(s, 0, t);
}
function k(e, t, o) {
  return A(e, t, o || v);
}
function v(e, t) {
  return e > t ? 1 : e < t ? -1 : 0;
}
function A(e, t, o) {
  let n = 0, s = e.length, r = 0, u = !1;
  for (; n < s; ) {
    r = n + (s - n >>> 1);
    const l = o(t, e[r]);
    l > 0 ? n = r + 1 : (s = r, u = !l);
  }
  return u ? n : -n - 1;
}
function O(e, t, o, n, s) {
  return y(
    e,
    t,
    o,
    n,
    s,
    0
    /* softNmsSigma */
  );
}
function P(e, t, o, n, s, r) {
  return y(
    e,
    t,
    o,
    n,
    s,
    0,
    !1,
    r,
    !0
    /* returnValidOutputs */
  );
}
function X(e, t, o, n, s, r) {
  return y(
    e,
    t,
    o,
    n,
    s,
    r,
    !0
    /* returnScoresTensor */
  );
}
function y(e, t, o, n, s, r, u = !1, l = !1, h = !1) {
  const c = [];
  for (let i = 0; i < t.length; i++)
    t[i] > s && c.push({ score: t[i], boxIndex: i, suppressBeginIndex: 0 });
  c.sort(C);
  const d = r > 0 ? -0.5 / r : 0, a = [], f = [];
  for (; a.length < o && c.length > 0; ) {
    const i = c.pop(), { score: M, boxIndex: I, suppressBeginIndex: g } = i;
    if (M < s)
      break;
    let S = !1;
    for (let b = a.length - 1; b >= g; --b) {
      const w = B(e, I, a[b]);
      if (w >= n) {
        S = !0;
        break;
      }
      if (i.score = i.score * V(n, d, w), i.score <= s)
        break;
    }
    i.suppressBeginIndex = a.length, S || (i.score === M ? (a.push(I), f.push(i.score)) : i.score > s && J(c, i, C));
  }
  const x = a.length, p = o - x;
  l && p > 0 && (a.push(...new Array(p).fill(0)), f.push(...new Array(p).fill(0)));
  const m = { selectedIndices: a };
  return u && (m.selectedScores = f), h && (m.validOutputs = x), m;
}
function B(e, t, o) {
  const n = e.subarray(t * 4, t * 4 + 4), s = e.subarray(o * 4, o * 4 + 4), r = Math.min(n[0], n[2]), u = Math.min(n[1], n[3]), l = Math.max(n[0], n[2]), h = Math.max(n[1], n[3]), c = Math.min(s[0], s[2]), d = Math.min(s[1], s[3]), a = Math.max(s[0], s[2]), f = Math.max(s[1], s[3]), x = (l - r) * (h - u), p = (a - c) * (f - d);
  if (x <= 0 || p <= 0)
    return 0;
  const m = Math.max(r, c), i = Math.max(u, d), M = Math.min(l, a), I = Math.min(h, f), g = Math.max(M - m, 0) * Math.max(I - i, 0);
  return g / (x + p - g);
}
function V(e, t, o) {
  const n = Math.exp(t * o * o);
  return o <= e ? n : 0;
}
function C(e, t) {
  return e.score - t.score || e.score === t.score && t.boxIndex - e.boxIndex;
}
export {
  X as a,
  P as b,
  O as n
};
