import { n as $ } from "./safeReact.js";
import "./constants.js";
import { o as W } from "./event.js";
var ht = [
  "top",
  "right",
  "bottom",
  "left"
], L = Math.min, F = Math.max, wt = Math.round, _ = Math.floor, mt = (t) => ({
  x: t,
  y: t
}), d = {
  left: "right",
  right: "left",
  bottom: "top",
  top: "bottom"
};
function Rt(t, r, a) {
  return F(t, L(r, a));
}
function At(t, r) {
  return typeof t == "function" ? t(r) : t;
}
function Q(t) {
  return t.split("-")[0];
}
function X(t) {
  return t.split("-")[1];
}
function p(t) {
  return t === "x" ? "y" : "x";
}
function x(t) {
  return t === "y" ? "height" : "width";
}
function v(t) {
  const r = t[0];
  return r === "t" || r === "b" ? "y" : "x";
}
function I(t) {
  return p(v(t));
}
function yt(t, r, a) {
  a === void 0 && (a = !1);
  const o = X(t), e = I(t), u = x(e);
  let f = e === "x" ? o === (a ? "end" : "start") ? "right" : "left" : o === "start" ? "bottom" : "top";
  return r.reference[u] > r.floating[u] && (f = j(f)), [f, j(f)];
}
function bt(t) {
  const r = j(t);
  return [
    T(t),
    r,
    T(r)
  ];
}
function T(t) {
  return t.includes("start") ? t.replace("start", "end") : t.replace("end", "start");
}
var q = ["left", "right"], J = ["right", "left"], tt = ["top", "bottom"], rt = ["bottom", "top"];
function it(t, r, a) {
  switch (t) {
    case "top":
    case "bottom":
      return a ? r ? J : q : r ? q : J;
    case "left":
    case "right":
      return r ? tt : rt;
    default:
      return [];
  }
}
function Ct(t, r, a, o) {
  const e = X(t);
  let u = it(Q(t), a === "start", o);
  return e && (u = u.map((f) => f + "-" + e), r && (u = u.concat(u.map(T)))), u;
}
function j(t) {
  const r = Q(t);
  return d[r] + t.slice(r.length);
}
function nt(t) {
  return {
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
    ...t
  };
}
function Ot(t) {
  return typeof t != "number" ? nt(t) : {
    top: t,
    right: t,
    bottom: t,
    left: t
  };
}
function Mt(t) {
  const { x: r, y: a, width: o, height: e } = t;
  return {
    width: o,
    height: e,
    top: a,
    left: r,
    right: r + o,
    bottom: a + e,
    x: r,
    y: a
  };
}
function k(t, r, a) {
  return Math.floor(t / r) !== a;
}
function K(t, r) {
  return r < 0 || r >= t.length;
}
function Pt(t, r) {
  return y(t.current, { disabledIndices: r });
}
function Et(t, r) {
  return y(t.current, {
    decrement: !0,
    startingIndex: t.current.length,
    disabledIndices: r
  });
}
function y(t, { startingIndex: r = -1, decrement: a = !1, disabledIndices: o, amount: e = 1 } = {}) {
  let u = r;
  do
    u += a ? -e : e;
  while (u >= 0 && u <= t.length - 1 && N(t, u, o));
  return u;
}
function St(t, { event: r, orientation: a, loopFocus: o, onLoop: e, rtl: u, cols: f, disabledIndices: w, minIndex: E, maxIndex: g, prevIndex: i, stopEvent: C = !1 }) {
  let n = i, b;
  if (r.key === "ArrowUp" ? b = "up" : r.key === "ArrowDown" && (b = "down"), b) {
    const c = [], S = [];
    let z = !1, B = 0;
    {
      let m = null, s = -1;
      t.forEach((R, O) => {
        if (R == null) return;
        B += 1;
        const l = R.closest('[role="row"]');
        l && (z = !0), (l !== m || s === -1) && (m = l, s += 1, c[s] = []), c[s].push(O), S[O] = s;
      });
    }
    let G = !1, V = 0;
    if (z) for (const m of c) {
      const s = m.length;
      s > V && (V = s), s !== f && (G = !0);
    }
    const H = G && B < t.length, h = V || f, Y = (m) => {
      if (!G || i === -1) return;
      const s = S[i];
      if (s == null) return;
      const R = c[s].indexOf(i), O = m === "up" ? -1 : 1;
      for (let l = s + O, A = 0; A < c.length; A += 1, l += O) {
        if (l < 0 || l >= c.length) {
          if (!o || H) return;
          if (l = l < 0 ? c.length - 1 : 0, e) {
            const M = Math.min(R, c[l].length - 1);
            l = S[e(r, i, c[l][M] ?? c[l][0])] ?? l;
          }
        }
        const D = c[l];
        for (let M = Math.min(R, D.length - 1); M >= 0; M -= 1) {
          const P = D[M];
          if (!N(t, P, w)) return P;
        }
      }
    }, Z = (m) => {
      if (!H || i === -1) return;
      const s = i % h, R = m === "up" ? -h : h, O = g - g % h, l = _(g / h) + 1;
      for (let A = i - s + R, D = 0; D < l; D += 1, A += R) {
        if (A < 0 || A > g) {
          if (!o) return;
          A = A < 0 ? O : 0;
        }
        const M = Math.min(A + h - 1, g);
        for (let P = Math.min(A + s, M); P >= A; P -= 1) if (!N(t, P, w)) return P;
      }
    };
    C && W(r);
    const U = Y(b) ?? Z(b);
    if (U !== void 0) n = U;
    else if (i === -1) n = b === "up" ? g : E;
    else if (n = y(t, {
      startingIndex: i,
      amount: h,
      decrement: b === "up",
      disabledIndices: w
    }), o) {
      if (b === "up" && (i - h < E || n < 0)) {
        const m = i % h, s = g % h, R = g - (s - m);
        s === m ? n = g : n = s > m ? R : R - h, e && (n = e(r, i, n));
      }
      b === "down" && i + h > g && (n = y(t, {
        startingIndex: i % h - h,
        amount: h,
        disabledIndices: w
      }), e && (n = e(r, i, n)));
    }
    K(t, n) && (n = i);
  }
  if (a === "both") {
    const c = _(i / f);
    r.key === (u ? "ArrowLeft" : "ArrowRight") && (C && W(r), i % f !== f - 1 ? (n = y(t, {
      startingIndex: i,
      disabledIndices: w
    }), o && k(n, f, c) && (n = y(t, {
      startingIndex: i - i % f - 1,
      disabledIndices: w
    }), e && (n = e(r, i, n)))) : o && (n = y(t, {
      startingIndex: i - i % f - 1,
      disabledIndices: w
    }), e && (n = e(r, i, n))), k(n, f, c) && (n = i)), r.key === (u ? "ArrowRight" : "ArrowLeft") && (C && W(r), i % f !== 0 ? (n = y(t, {
      startingIndex: i,
      decrement: !0,
      disabledIndices: w
    }), o && k(n, f, c) && (n = y(t, {
      startingIndex: i + (f - i % f),
      decrement: !0,
      disabledIndices: w
    }), e && (n = e(r, i, n)))) : o && (n = y(t, {
      startingIndex: i + (f - i % f),
      decrement: !0,
      disabledIndices: w
    }), e && (n = e(r, i, n))), k(n, f, c) && (n = i));
    const S = _(g / f) === c;
    K(t, n) && (o && S ? (n = r.key === (u ? "ArrowRight" : "ArrowLeft") ? g : y(t, {
      startingIndex: i - i % f - 1,
      disabledIndices: w
    }), e && (n = e(r, i, n))) : n = i);
  }
  return n;
}
function Dt(t, r, a) {
  const o = [];
  let e = 0;
  return t.forEach(({ width: u, height: f }, w) => {
    u > r;
    let E = !1;
    for (a && (e = 0); !E; ) {
      const g = [];
      for (let i = 0; i < u; i += 1) for (let C = 0; C < f; C += 1) g.push(e + i + C * r);
      e % r + u <= r && g.every((i) => o[i] == null) ? (g.forEach((i) => {
        o[i] = w;
      }), E = !0) : e += 1;
    }
  }), [...o];
}
function kt(t, r, a, o, e) {
  if (t === -1) return -1;
  const u = a.indexOf(t), f = r[t];
  switch (e) {
    case "tl":
      return u;
    case "tr":
      return f ? u + f.width - 1 : u;
    case "bl":
      return f ? u + (f.height - 1) * o : u;
    case "br":
      return a.lastIndexOf(t);
    default:
      return -1;
  }
}
function Gt(t, r) {
  return r.flatMap((a, o) => t.includes(a) ? [o] : []);
}
function N(t, r, a) {
  if (typeof a == "function" ? a(r) : a?.includes(r) ?? !1) return !0;
  const o = t[r];
  return o ? ft(o) ? !a && (o.hasAttribute("disabled") || o.getAttribute("aria-disabled") === "true") : !0 : !1;
}
function et(t) {
  return t.visibility === "hidden" || t.visibility === "collapse";
}
function ft(t, r = t ? $(t) : null) {
  return !t || !t.isConnected || !r || et(r) ? !1 : typeof t.checkVisibility == "function" ? t.checkVisibility() : r.display !== "none" && r.display !== "contents";
}
export {
  ht as A,
  Ot as C,
  L as D,
  F as E,
  Mt as O,
  j as S,
  v as T,
  yt as _,
  St as a,
  p as b,
  ft as c,
  Rt as d,
  mt as f,
  I as g,
  X as h,
  Gt as i,
  wt as k,
  K as l,
  _ as m,
  y as n,
  Et as o,
  At as p,
  kt as r,
  Pt as s,
  Dt as t,
  N as u,
  x as v,
  Q as w,
  Ct as x,
  bt as y
};

//# sourceMappingURL=composite.js.map