// packages/components/src/tree-grid/roving-tab-index-item.tsx
import { useRef, forwardRef } from "@wordpress/element";
import { useRovingTabIndexContext } from "./roving-tab-index-context.js";
import { jsx as _jsx } from "react/jsx-runtime";
var RovingTabIndexItem = forwardRef(function UnforwardedRovingTabIndexItem({
  children,
  as: Component,
  ...props
}, forwardedRef) {
  const localRef = useRef();
  const ref = forwardedRef || localRef;
  const {
    lastFocusedElement,
    setLastFocusedElement
  } = useRovingTabIndexContext();
  let tabIndex;
  if (lastFocusedElement) {
    tabIndex = lastFocusedElement === // TODO: The original implementation simply used `ref.current` here, assuming
    // that a forwarded ref would always be an object, which is not necessarily true.
    // This workaround maintains the original runtime behavior in a type-safe way,
    // but should be revisited.
    ("current" in ref ? ref.current : void 0) ? 0 : -1;
  }
  const onFocus = (event) => setLastFocusedElement?.(event.target);
  const allProps = {
    ref,
    tabIndex,
    onFocus,
    ...props
  };
  if (typeof children === "function") {
    return children(allProps);
  }
  if (!Component) {
    return null;
  }
  return /* @__PURE__ */ _jsx(Component, {
    ...allProps,
    children
  });
});
var roving_tab_index_item_default = RovingTabIndexItem;
export {
  RovingTabIndexItem,
  roving_tab_index_item_default as default
};
//# sourceMappingURL=roving-tab-index-item.js.map
