import { useRef as D, useCallback as w } from "react";
function R({
  items: a,
  activeIndex: o,
  onActivate: s,
  orientation: u,
  loop: c = !0
}) {
  const f = D(/* @__PURE__ */ new Map()), p = w(
    (e) => {
      s(e);
      const n = f.current.get(e);
      n == null || n.focus();
    },
    [s]
  );
  return { getItemProps: w(
    (e) => {
      const n = u === "horizontal" ? "ArrowRight" : "ArrowDown", k = u === "horizontal" ? "ArrowLeft" : "ArrowUp";
      return {
        tabIndex: e === o ? 0 : -1,
        "aria-selected": e === o,
        onKeyDown: (r) => {
          const l = a.length;
          let t = null;
          switch (r.key) {
            case n: {
              r.preventDefault(), e < l - 1 ? t = e + 1 : c && (t = 0);
              break;
            }
            case k: {
              r.preventDefault(), e > 0 ? t = e - 1 : c && (t = l - 1);
              break;
            }
            case "Home": {
              r.preventDefault(), t = 0;
              break;
            }
            case "End": {
              r.preventDefault(), t = l - 1;
              break;
            }
            case "Enter":
            case " ": {
              r.preventDefault(), s(e);
              return;
            }
            default:
              return;
          }
          t !== null && p(t);
        },
        onClick: () => {
          s(e);
        },
        ref: (r) => {
          r ? f.current.set(e, r) : f.current.delete(e);
        }
      };
    },
    [a.length, o, s, u, c, p]
  ) };
}
export {
  R as useRovingTabIndex
};
//# sourceMappingURL=useRovingTabIndex.js.map
