{"version":3,"file":"ScrollBehavior.js","names":[],"sources":["esm/views/ScrollBehavior.js"],"sourcesContent":["import { useIsFocused } from \"@react-navigation/core\";\nimport { useContext, useEffect, useLayoutEffect } from \"react\";\nimport { RouteInfoContext } from \"../router/RouteInfoContext\";\nimport { setLastAction } from \"../router/lastAction\";\nimport {\n  routeInfo,\n  subscribeToLoadingState,\n  subscribeToRootState\n} from \"../router/router\";\nconst KEY = \"one-sr\";\nconst GROUP_KEY = \"one-sr-groups\";\nconst getState = () => JSON.parse(sessionStorage.getItem(KEY) || \"{}\");\nconst getGroupState = () => JSON.parse(sessionStorage.getItem(GROUP_KEY) || \"{}\");\nlet activeGroups = /* @__PURE__ */ new Set();\nfunction registerScrollGroup(groupId) {\n  activeGroups.add(groupId);\n  return () => {\n    activeGroups.delete(groupId);\n  };\n}\nfunction getGroupKey(pathname) {\n  let longestMatch = null;\n  for (const group of activeGroups) {\n    if ((pathname === group || pathname.startsWith(`${group}/`)) && (!longestMatch || group.length > longestMatch.length)) {\n      longestMatch = group;\n    }\n  }\n  return longestMatch;\n}\nfunction restorePosition(pathname) {\n  try {\n    const positions = getState();\n    const saved = positions[pathname];\n    if (typeof saved === \"number\") {\n      setTimeout(() => {\n        window.scrollTo(0, saved);\n      });\n    }\n  } catch (error) {\n    console.error(`Error restoring scroll position`, error);\n    sessionStorage.removeItem(KEY);\n  }\n}\nfunction restoreGroupPosition(groupId) {\n  try {\n    const positions = getGroupState();\n    const saved = positions[groupId];\n    if (typeof saved === \"number\") {\n      setTimeout(() => {\n        window.scrollTo(0, saved);\n      });\n    }\n  } catch (error) {\n    console.error(`Error restoring scroll position for group ${groupId}`, error);\n    sessionStorage.removeItem(GROUP_KEY);\n  }\n}\nlet didPop = false;\nlet previousPathname = null;\nfunction rememberScrollPosition() {\n  didPop = false;\n  const pathname = window.location.pathname;\n  const state = getState();\n  state[pathname] = window.scrollY;\n  sessionStorage.setItem(KEY, JSON.stringify(state));\n  const groupKey = getGroupKey(pathname);\n  if (groupKey) {\n    const groupState = getGroupState();\n    groupState[groupKey] = window.scrollY;\n    sessionStorage.setItem(GROUP_KEY, JSON.stringify(groupState));\n  }\n  previousPathname = pathname;\n}\nlet disable = null;\nlet pendingScroll = null;\nlet committedHref;\nfunction ScrollBehaviorRouteCommit() {\n  const info = useContext(RouteInfoContext);\n  const href = info?.unstable_globalHref;\n  const focused = useIsFocused();\n  useLayoutEffect(() => {\n    if (!focused) return;\n    committedHref = href;\n    if (pendingScroll && pendingScroll.href === href) {\n      const { apply } = pendingScroll;\n      pendingScroll = null;\n      apply();\n    }\n  });\n  return null;\n}\nfunction configure(props) {\n  if (typeof window === \"undefined\" || !window.addEventListener) {\n    return;\n  }\n  disable?.();\n  const initialLocation = `${window.location.pathname}${window.location.search}`;\n  let isFirstStateChange = true;\n  let handledLocation = initialLocation;\n  previousPathname = window.location.pathname;\n  const popStateController = new AbortController();\n  window.addEventListener(\n    \"popstate\",\n    () => {\n      didPop = true;\n      setLastAction();\n    },\n    {\n      signal: popStateController.signal\n    }\n  );\n  const disposeOnLoadState = subscribeToLoadingState((state) => {\n    if (state === \"loading\") {\n      rememberScrollPosition();\n    }\n  });\n  const disposeOnRootState = subscribeToRootState((state) => {\n    const currentHref = routeInfo?.unstable_globalHref ?? \"\";\n    const currentPathname = routeInfo?.pathname ?? \"\";\n    if (isFirstStateChange) {\n      isFirstStateChange = false;\n      if (currentHref === initialLocation) {\n        return;\n      }\n    }\n    const { hash } = state;\n    const location = `${currentHref}${hash || \"\"}`;\n    if (location === handledLocation) {\n      pendingScroll = null;\n      return;\n    }\n    pendingScroll = null;\n    if (state.linkOptions?.scroll === false) {\n      handledLocation = location;\n      previousPathname = currentPathname;\n      return;\n    }\n    const restoring = didPop;\n    const prevGroup = previousPathname ? getGroupKey(previousPathname) : null;\n    const currentGroup = getGroupKey(currentPathname);\n    const scrollGroup = state.linkOptions?.scrollGroup;\n    pendingScroll = {\n      href: currentHref,\n      apply: () => {\n        handledLocation = location;\n        previousPathname = currentPathname;\n        if (hash) {\n          scrollToHash(hash);\n        } else if (restoring) {\n          if (props.disable !== \"restore\") {\n            restorePosition(currentPathname);\n          }\n        } else if (prevGroup && currentGroup && prevGroup === currentGroup) {\n          restoreGroupPosition(currentGroup);\n        } else if (scrollGroup) {\n          restoreGroupPosition(scrollGroup);\n        } else {\n          window.scrollTo(0, 0);\n        }\n      }\n    };\n    if (committedHref === currentHref) {\n      const { apply } = pendingScroll;\n      pendingScroll = null;\n      apply();\n    }\n  });\n  disable = () => {\n    pendingScroll = null;\n    popStateController.abort();\n    disposeOnLoadState();\n    disposeOnRootState();\n  };\n  return disable;\n}\nfunction scrollToHash(hash) {\n  if (!hash || !hash.startsWith(\"#\")) return;\n  const id = hash.slice(1);\n  const el = document.getElementById(id);\n  if (!el) return;\n  el.scrollIntoView({ behavior: \"instant\" });\n}\nfunction ScrollBehavior(props) {\n  if (process.env.VITE_ENVIRONMENT === \"client\") {\n    useEffect(() => {\n      if (window.location.hash) {\n        scrollToHash(window.location.hash);\n      }\n    }, []);\n    useEffect(() => {\n      return configure(props);\n    }, [props.disable]);\n  }\n  return null;\n}\nexport {\n  ScrollBehavior,\n  ScrollBehaviorRouteCommit,\n  registerScrollGroup\n};\n//# sourceMappingURL=ScrollBehavior.js.map\n"],"mappings":";;;;;;;AASA,MAAM,MAAM;AACZ,MAAM,YAAY;AAClB,MAAM,iBAAiB,KAAK,MAAM,eAAe,QAAQ,GAAG,KAAK,IAAI;AACrE,MAAM,sBAAsB,KAAK,MAAM,eAAe,QAAQ,SAAS,KAAK,IAAI;AAChF,IAAI,+BAA+B,IAAI,IAAI;AAC3C,SAAS,oBAAoB,SAAS;CACpC,aAAa,IAAI,OAAO;CACxB,aAAa;EACX,aAAa,OAAO,OAAO;CAC7B;AACF;AACA,SAAS,YAAY,UAAU;CAC7B,IAAI,eAAe;CACnB,KAAK,MAAM,SAAS,cAAc;EAChC,KAAK,aAAa,SAAS,SAAS,WAAW,GAAG,MAAM,EAAE,OAAO,CAA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