import { f as V } from "./floating-ui.utils-48URp89E.js";
import { b as q } from "./floating-ui.utils.dom-jr2suqy2.js";
import { s as _ } from "./event-BCa0Un_3.js";
import { A as J, e as K, f as S, h as b } from "./element-DSo91iOi.js";
function W(t, e, s) {
  return Math.floor(t / e) !== s;
}
function B(t, e) {
  return e < 0 || e >= t.current.length;
}
function F(t, e) {
  return y(t, {
    disabledIndices: e
  });
}
function p(t, e) {
  return y(t, {
    decrement: !0,
    startingIndex: t.current.length,
    disabledIndices: e
  });
}
function y(t, {
  startingIndex: e = -1,
  decrement: s = !1,
  disabledIndices: u,
  amount: o = 1
} = {}) {
  let n = e;
  do
    n += s ? -o : o;
  while (n >= 0 && n <= t.current.length - 1 && j(t, n, u));
  return n;
}
function I(t, {
  event: e,
  orientation: s,
  loopFocus: u,
  rtl: o,
  cols: n,
  disabledIndices: a,
  minIndex: R,
  maxIndex: l,
  prevIndex: r,
  stopEvent: m = !1
}) {
  let i = r, O;
  if (e.key === J ? O = "up" : e.key === K && (O = "down"), O) {
    const g = [], L = [];
    let z = !1, P = 0;
    {
      let h = null, f = -1;
      t.current.forEach((C, D) => {
        if (C == null)
          return;
        P += 1;
        const w = C.closest('[role="row"]');
        w && (z = !0), (w !== h || f === -1) && (h = w, f += 1, g[f] = []), g[f].push(D), L[D] = f;
      });
    }
    let k = !1, N = 0;
    if (z)
      for (const h of g) {
        const f = h.length;
        f > N && (N = f), f !== n && (k = !0);
      }
    const T = k && P < t.current.length, c = N || n, H = (h) => {
      if (!k || r === -1)
        return;
      const f = L[r];
      if (f == null)
        return;
      const C = g[f].indexOf(r), D = h === "up" ? -1 : 1;
      for (let w = f + D, E = 0; E < g.length; E += 1, w += D) {
        if (w < 0 || w >= g.length) {
          if (!u || T)
            return;
          w = w < 0 ? g.length - 1 : 0;
        }
        const A = g[w];
        for (let G = Math.min(C, A.length - 1); G >= 0; G -= 1) {
          const M = A[G];
          if (!j(t, M, a))
            return M;
        }
      }
    }, $ = (h) => {
      if (!T || r === -1)
        return;
      const f = r % c, C = h === "up" ? -c : c, D = l - l % c, w = V(l / c) + 1;
      for (let E = r - f + C, A = 0; A < w; A += 1, E += C) {
        if (E < 0 || E > l) {
          if (!u)
            return;
          E = E < 0 ? D : 0;
        }
        const G = Math.min(E + c - 1, l);
        for (let M = Math.min(E + f, G); M >= E; M -= 1)
          if (!j(t, M, a))
            return M;
      }
    };
    m && _(e);
    const U = H(O) ?? $(O);
    if (U !== void 0)
      i = U;
    else if (r === -1)
      i = O === "up" ? l : R;
    else if (i = y(t, {
      startingIndex: r,
      amount: c,
      decrement: O === "up",
      disabledIndices: a
    }), u) {
      if (O === "up" && (r - c < R || i < 0)) {
        const h = r % c, f = l % c, C = l - (f - h);
        f === h ? i = l : i = f > h ? C : C - c;
      }
      O === "down" && r + c > l && (i = y(t, {
        startingIndex: r % c - c,
        amount: c,
        disabledIndices: a
      }));
    }
    B(t, i) && (i = r);
  }
  if (s === "both") {
    const g = V(r / n);
    e.key === (o ? S : b) && (m && _(e), r % n !== n - 1 ? (i = y(t, {
      startingIndex: r,
      disabledIndices: a
    }), u && W(i, n, g) && (i = y(t, {
      startingIndex: r - r % n - 1,
      disabledIndices: a
    }))) : u && (i = y(t, {
      startingIndex: r - r % n - 1,
      disabledIndices: a
    })), W(i, n, g) && (i = r)), e.key === (o ? b : S) && (m && _(e), r % n !== 0 ? (i = y(t, {
      startingIndex: r,
      decrement: !0,
      disabledIndices: a
    }), u && W(i, n, g) && (i = y(t, {
      startingIndex: r + (n - r % n),
      decrement: !0,
      disabledIndices: a
    }))) : u && (i = y(t, {
      startingIndex: r + (n - r % n),
      decrement: !0,
      disabledIndices: a
    })), W(i, n, g) && (i = r));
    const L = V(l / n) === g;
    B(t, i) && (u && L ? i = e.key === (o ? b : S) ? l : y(t, {
      startingIndex: r - r % n - 1,
      disabledIndices: a
    }) : i = r);
  }
  return i;
}
function v(t, e, s) {
  const u = [];
  let o = 0;
  return t.forEach(({
    width: n,
    height: a
  }, R) => {
    if (n > e && process.env.NODE_ENV !== "production")
      throw new Error(`[Floating UI]: Invalid grid - item width at index ${R} is greater than grid columns`);
    let l = !1;
    for (s && (o = 0); !l; ) {
      const r = [];
      for (let m = 0; m < n; m += 1)
        for (let i = 0; i < a; i += 1)
          r.push(o + m + i * e);
      o % e + n <= e && r.every((m) => u[m] == null) ? (r.forEach((m) => {
        u[m] = R;
      }), l = !0) : o += 1;
    }
  }), [...u];
}
function x(t, e, s, u, o) {
  if (t === -1)
    return -1;
  const n = s.indexOf(t), a = e[t];
  switch (o) {
    case "tl":
      return n;
    case "tr":
      return a ? n + a.width - 1 : n;
    case "bl":
      return a ? n + (a.height - 1) * u : n;
    case "br":
      return s.lastIndexOf(t);
    default:
      return -1;
  }
}
function tt(t, e) {
  return e.flatMap((s, u) => t.includes(s) ? [u] : []);
}
function j(t, e, s) {
  if (typeof s == "function" ? s(e) : s?.includes(e) ?? !1)
    return !0;
  const o = t.current[e];
  return o ? Q(o) ? !s && (o.hasAttribute("disabled") || o.getAttribute("aria-disabled") === "true") : !0 : !1;
}
function Q(t) {
  return q(t).display !== "none";
}
export {
  p as a,
  j as b,
  v as c,
  I as d,
  x as e,
  tt as f,
  F as g,
  y as h,
  B as i,
  Q as j
};
