import U from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/formatErrorMessage.js";
import { floor as B } from "../../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.js";
import { stopEvent as y } from "./event.js";
import { ARROW_UP as j, ARROW_DOWN as z, ARROW_LEFT as A, ARROW_RIGHT as G } from "./constants.js";
function M(r, n, g) {
  return Math.floor(r / n) !== g;
}
function L(r, n) {
  return n < 0 || n >= r.current.length;
}
function J(r, n) {
  return c(r, {
    disabledIndices: n
  });
}
function K(r, n) {
  return c(r, {
    decrement: !0,
    startingIndex: r.current.length,
    disabledIndices: n
  });
}
function c(r, {
  startingIndex: n = -1,
  decrement: g = !1,
  disabledIndices: u,
  amount: w = 1
} = {}) {
  let t = n;
  do
    t += g ? -w : w;
  while (t >= 0 && t <= r.current.length - 1 && V(r, t, u));
  return t;
}
function Q(r, {
  event: n,
  orientation: g,
  loopFocus: u,
  rtl: w,
  cols: t,
  disabledIndices: o,
  minIndex: O,
  maxIndex: s,
  prevIndex: e,
  stopEvent: l = !1
}) {
  let f = e;
  const m = [], _ = {};
  let k = !1;
  {
    let a = null, h = -1;
    r.current.forEach((E, i) => {
      if (E == null)
        return;
      const C = E.closest('[role="row"]');
      C && (k = !0), (C !== a || h === -1) && (a = C, h += 1, m[h] = []), m[h].push(i), _[i] = h;
    });
  }
  const P = k && m.length > 0 && m.some((a) => a.length !== t);
  function R(a) {
    if (!P || e === -1)
      return;
    const h = _[e];
    if (h == null)
      return;
    const E = m[h].indexOf(e);
    let i = a === "up" ? h - 1 : h + 1;
    u && (i < 0 ? i = m.length - 1 : i >= m.length && (i = 0));
    const C = /* @__PURE__ */ new Set();
    for (; i >= 0 && i < m.length && !C.has(i); ) {
      C.add(i);
      const D = m[i];
      if (D.length === 0) {
        i = a === "up" ? i - 1 : i + 1;
        continue;
      }
      const T = Math.min(E, D.length - 1);
      for (let N = T; N >= 0; N -= 1) {
        const W = D[N];
        if (!V(r, W, o))
          return W;
      }
      i = a === "up" ? i - 1 : i + 1, u && (i < 0 ? i = m.length - 1 : i >= m.length && (i = 0));
    }
  }
  if (n.key === j) {
    const a = R("up");
    if (a !== void 0)
      l && y(n), f = a;
    else {
      if (l && y(n), e === -1)
        f = s;
      else if (f = c(r, {
        startingIndex: f,
        amount: t,
        decrement: !0,
        disabledIndices: o
      }), u && (e - t < O || f < 0)) {
        const h = e % t, E = s % t, i = s - (E - h);
        E === h ? f = s : f = E > h ? i : i - t;
      }
      L(r, f) && (f = e);
    }
  }
  if (n.key === z) {
    const a = R("down");
    a !== void 0 ? (l && y(n), f = a) : (l && y(n), e === -1 ? f = O : (f = c(r, {
      startingIndex: e,
      amount: t,
      disabledIndices: o
    }), u && e + t > s && (f = c(r, {
      startingIndex: e % t - t,
      amount: t,
      disabledIndices: o
    }))), L(r, f) && (f = e));
  }
  if (g === "both") {
    const a = B(e / t);
    n.key === (w ? A : G) && (l && y(n), e % t !== t - 1 ? (f = c(r, {
      startingIndex: e,
      disabledIndices: o
    }), u && M(f, t, a) && (f = c(r, {
      startingIndex: e - e % t - 1,
      disabledIndices: o
    }))) : u && (f = c(r, {
      startingIndex: e - e % t - 1,
      disabledIndices: o
    })), M(f, t, a) && (f = e)), n.key === (w ? G : A) && (l && y(n), e % t !== 0 ? (f = c(r, {
      startingIndex: e,
      decrement: !0,
      disabledIndices: o
    }), u && M(f, t, a) && (f = c(r, {
      startingIndex: e + (t - e % t),
      decrement: !0,
      disabledIndices: o
    }))) : u && (f = c(r, {
      startingIndex: e + (t - e % t),
      decrement: !0,
      disabledIndices: o
    })), M(f, t, a) && (f = e));
    const h = B(s / t) === a;
    L(r, f) && (u && h ? f = n.key === (w ? G : A) ? s : c(r, {
      startingIndex: e - e % t - 1,
      disabledIndices: o
    }) : f = e);
  }
  return f;
}
function X(r, n, g) {
  const u = [];
  let w = 0;
  return r.forEach(({
    width: t,
    height: o
  }, O) => {
    if (t > n && process.env.NODE_ENV !== "production")
      throw new Error(process.env.NODE_ENV !== "production" ? `[Floating UI]: Invalid grid - item width at index ${O} is greater than grid columns` : U(29, O));
    let s = !1;
    for (g && (w = 0); !s; ) {
      const e = [];
      for (let l = 0; l < t; l += 1)
        for (let f = 0; f < o; f += 1)
          e.push(w + l + f * n);
      w % n + t <= n && e.every((l) => u[l] == null) ? (e.forEach((l) => {
        u[l] = O;
      }), s = !0) : w += 1;
    }
  }), [...u];
}
function Y(r, n, g, u, w) {
  if (r === -1)
    return -1;
  const t = g.indexOf(r), o = n[r];
  switch (w) {
    case "tl":
      return t;
    case "tr":
      return o ? t + o.width - 1 : t;
    case "bl":
      return o ? t + (o.height - 1) * u : t;
    case "br":
      return g.lastIndexOf(r);
    default:
      return -1;
  }
}
function Z(r, n) {
  return n.flatMap((g, u) => r.includes(g) ? [u] : []);
}
function V(r, n, g) {
  if (typeof g == "function")
    return g(n);
  if (g)
    return g.includes(n);
  const u = r.current[n];
  return u ? u.hasAttribute("disabled") || u.getAttribute("aria-disabled") === "true" : !1;
}
export {
  X as createGridCellMap,
  c as findNonDisabledListIndex,
  Y as getGridCellIndexOfCorner,
  Z as getGridCellIndices,
  Q as getGridNavigatedIndex,
  K as getMaxListIndex,
  J as getMinListIndex,
  M as isDifferentGridRow,
  L as isIndexOutOfListBounds,
  V as isListIndexDisabled
};
