import { RouterProvider } from './chunk-Q7DJKLEN.js';
import { useLocation, useRouter, Link } from '@tanstack/react-router';
import { useCallback, useMemo, forwardRef, useRef, useEffect } from 'react';
import { jsx } from 'react/jsx-runtime';

function TanStackRouterBridge({ children }) {
  const location = useLocation();
  const router = useRouter();
  const navigate = useCallback((to) => router.navigate({ to }), [router]);
  const Link$1 = useMemo(
    () => forwardRef(function AdapterLink(props, ref) {
      return /* @__PURE__ */ jsx(Link, { ref, ...props });
    }),
    []
  );
  const value = useMemo(
    () => ({
      currentPath: location.pathname,
      currentSearch: location.searchStr ?? "",
      navigate,
      Link: Link$1
    }),
    [location.pathname, location.searchStr, navigate, Link$1]
  );
  return /* @__PURE__ */ jsx(RouterProvider, { value, children });
}
function ScrollToTop() {
  const location = useLocation();
  const anchorRef = useRef(null);
  useEffect(() => {
    const el = anchorRef.current;
    if (!el) return;
    let parent = el.parentElement;
    while (parent) {
      const overflowY = getComputedStyle(parent).overflowY;
      const isScrollable = overflowY === "auto" || overflowY === "scroll";
      if (isScrollable && parent.scrollHeight > parent.clientHeight) {
        parent.scrollTo({ top: 0, behavior: "auto" });
        return;
      }
      parent = parent.parentElement;
    }
    window.scrollTo(0, 0);
  }, [location.pathname]);
  return /* @__PURE__ */ jsx("div", { ref: anchorRef, "aria-hidden": true, style: { display: "none" } });
}

export { ScrollToTop, TanStackRouterBridge };
