{"version":3,"file":"interceptRoutes.js","names":[],"sources":["esm/router/interceptRoutes.js"],"sourcesContent":["import {\n  matchDynamicName,\n  matchRoutePattern,\n  stripGroupSegmentsFromPath\n} from \"./matchers\";\nimport { hasWebHistory, isNative } from \"../constants\";\nlet navigationMode = \"hard\";\nfunction setNavigationType(type) {\n  navigationMode = type;\n}\nfunction getNavigationType() {\n  return navigationMode;\n}\nfunction isHardNavigation() {\n  return navigationMode === \"hard\";\n}\nfunction collectAllLayoutsWithSlots(node, collected = []) {\n  if (node.slots && node.slots.size > 0) {\n    collected.push(node);\n  }\n  if (node.children) {\n    for (const child of node.children) {\n      collectAllLayoutsWithSlots(child, collected);\n    }\n  }\n  return collected;\n}\nfunction getLayoutPath(node) {\n  let path = node.contextKey.replace(/^\\.\\//, \"\").replace(/\\/?_layout.*$/, \"\").replace(/^app\\/?/, \"\");\n  path = stripGroupSegmentsFromPath(path);\n  return \"/\" + path;\n}\nfunction isLayoutAncestorOfPath(layoutPath, currentPath) {\n  if (!layoutPath.replace(/\\/+$/, \"\")) return true;\n  return matchRoutePattern(layoutPath, currentPath) !== null;\n}\nfunction findLayoutsWithSlotsAlongPath(rootNode, currentPath) {\n  if (!rootNode) return [];\n  const allLayoutsWithSlots = collectAllLayoutsWithSlots(rootNode);\n  const ancestorLayouts = allLayoutsWithSlots.filter((layout) => {\n    const layoutPath = getLayoutPath(layout);\n    return isLayoutAncestorOfPath(layoutPath, currentPath);\n  });\n  ancestorLayouts.sort((a, b) => {\n    const depthA = getLayoutPath(a).split(\"/\").filter(Boolean).length;\n    const depthB = getLayoutPath(b).split(\"/\").filter(Boolean).length;\n    return depthA - depthB;\n  });\n  return ancestorLayouts;\n}\nfunction findInterceptRoute(targetPath, rootNode, currentPath) {\n  if (isNative) {\n    return null;\n  }\n  if (isHardNavigation()) {\n    return null;\n  }\n  const layoutsWithSlots = findLayoutsWithSlotsAlongPath(rootNode, currentPath);\n  if (layoutsWithSlots.length === 0) {\n    return null;\n  }\n  for (let i = layoutsWithSlots.length - 1; i >= 0; i--) {\n    const layoutNode = layoutsWithSlots[i];\n    for (const [slotName, slotConfig] of layoutNode.slots) {\n      const result = findMatchingInterceptInSlot(\n        targetPath,\n        slotName,\n        slotConfig,\n        layoutNode,\n        currentPath\n      );\n      if (result) {\n        return result;\n      }\n    }\n  }\n  return null;\n}\nfunction findMatchingInterceptInSlot(targetPath, slotName, slotConfig, layoutNode, currentPath) {\n  for (const interceptRoute of slotConfig.interceptRoutes) {\n    if (!interceptRoute.intercept) {\n      continue;\n    }\n    const { levels, targetPath: interceptTargetPath } = interceptRoute.intercept;\n    const resolvedTargetPath = resolveInterceptTargetPath(\n      interceptTargetPath,\n      levels,\n      layoutNode\n    );\n    const params = matchPath(targetPath, resolvedTargetPath);\n    if (params !== null) {\n      return {\n        interceptRoute,\n        slotName,\n        layoutContextKey: layoutNode.contextKey,\n        params\n      };\n    }\n  }\n  return null;\n}\nfunction resolveInterceptTargetPath(interceptTargetPath, levels, layoutNode) {\n  const fullLayoutPath = getLayoutPath(layoutNode);\n  let layoutPath = fullLayoutPath === \"/\" ? \"\" : fullLayoutPath.slice(1);\n  if (levels === Infinity) {\n    return \"/\" + interceptTargetPath;\n  }\n  if (levels === 0) {\n    const basePath = layoutPath ? \"/\" + layoutPath : \"\";\n    return basePath + \"/\" + interceptTargetPath;\n  }\n  const pathParts = layoutPath.split(\"/\").filter(Boolean);\n  const parentParts = pathParts.slice(0, -levels);\n  const parentPath = parentParts.length > 0 ? \"/\" + parentParts.join(\"/\") : \"\";\n  return parentPath + \"/\" + interceptTargetPath;\n}\nfunction matchPath(path, pattern) {\n  const pathname = path.split(/[?#]/, 1)[0];\n  const normalizedPath = \"/\" + pathname.replace(/^\\/+/, \"\").replace(/\\/+$/, \"\");\n  const normalizedPattern = \"/\" + pattern.replace(/^\\/+/, \"\").replace(/\\/+$/, \"\");\n  const pathParts = normalizedPath.split(\"/\").filter(Boolean);\n  const patternParts = normalizedPattern.split(\"/\").filter(Boolean);\n  const params = {};\n  let pathIndex = 0;\n  for (let i = 0; i < patternParts.length; i++) {\n    const patternPart = patternParts[i];\n    const dynamicMatch = matchDynamicName(patternPart);\n    if (dynamicMatch) {\n      if (dynamicMatch.deep) {\n        const remaining = pathParts.slice(pathIndex);\n        if (remaining.length === 0) {\n          return null;\n        }\n        params[dynamicMatch.name] = remaining.join(\"/\");\n        return params;\n      } else {\n        if (pathIndex >= pathParts.length) {\n          return null;\n        }\n        params[dynamicMatch.name] = pathParts[pathIndex];\n        pathIndex++;\n      }\n    } else {\n      if (pathIndex >= pathParts.length || pathParts[pathIndex] !== patternPart) {\n        return null;\n      }\n      pathIndex++;\n    }\n  }\n  if (pathIndex !== pathParts.length) {\n    return null;\n  }\n  return params;\n}\nlet preInterceptUrl = null;\nfunction updateURLWithoutNavigation(href) {\n  if (hasWebHistory) {\n    preInterceptUrl = window.location.pathname + window.location.search;\n    window.history.pushState(\n      {\n        __intercepted: true,\n        __actualPath: href,\n        __preInterceptUrl: preInterceptUrl\n      },\n      \"\",\n      href\n    );\n  }\n}\nlet clearSlotStatesCallback = null;\nfunction registerClearSlotStates(callback) {\n  clearSlotStatesCallback = callback;\n}\nfunction closeIntercept() {\n  if (!hasWebHistory) return false;\n  const state = window.history.state;\n  if (!state?.__intercepted) {\n    return false;\n  }\n  returningFromIntercept = true;\n  clearSlotStatesCallback?.();\n  window.history.back();\n  return true;\n}\nfunction isInterceptedNavigation() {\n  if (!hasWebHistory) return false;\n  return window.history.state?.__intercepted === true;\n}\nfunction getInterceptedActualPath() {\n  if (!hasWebHistory) return null;\n  return window.history.state?.__actualPath ?? null;\n}\nfunction getPreInterceptUrl() {\n  if (!hasWebHistory) return null;\n  return window.history.state?.__preInterceptUrl ?? preInterceptUrl;\n}\nlet returningFromIntercept = false;\nfunction setReturningFromIntercept(value) {\n  returningFromIntercept = value;\n}\nfunction isReturningFromIntercept() {\n  return returningFromIntercept;\n}\nlet setSlotStateCallback = null;\nfunction registerSetSlotState(callback) {\n  setSlotStateCallback = callback;\n}\nlet lastInterceptRouteNode = null;\nlet lastInterceptSlotName = null;\nlet lastInterceptParams = null;\nfunction storeInterceptState(slotName, routeNode, params) {\n  lastInterceptSlotName = slotName;\n  lastInterceptRouteNode = routeNode;\n  lastInterceptParams = params;\n}\nfunction restoreInterceptFromHistory() {\n  if (!hasWebHistory) return false;\n  const state = window.history.state;\n  if (!state?.__intercepted) {\n    return false;\n  }\n  if (lastInterceptRouteNode && lastInterceptSlotName && setSlotStateCallback) {\n    setSlotStateCallback(lastInterceptSlotName, {\n      activeRouteKey: lastInterceptRouteNode.contextKey,\n      activeRouteNode: lastInterceptRouteNode,\n      params: lastInterceptParams || {},\n      isIntercepted: true\n    });\n    return true;\n  }\n  return false;\n}\nexport {\n  closeIntercept,\n  findInterceptRoute,\n  getInterceptedActualPath,\n  getNavigationType,\n  getPreInterceptUrl,\n  isHardNavigation,\n  isInterceptedNavigation,\n  isReturningFromIntercept,\n  registerClearSlotStates,\n  registerSetSlotState,\n  restoreInterceptFromHistory,\n  setNavigationType,\n  setReturningFromIntercept,\n  storeInterceptState,\n  updateURLWithoutNavigation\n};\n//# sourceMappingURL=interceptRoutes.js.map\n"],"mappings":";;;;AAMA,IAAI,iBAAiB;AACrB,SAAS,kBAAkB,MAAM;CAC/B,iBAAiB;AACnB;AACA,SAAS,oBAAoB;CAC3B,OAAO;AACT;AACA,SAAS,mBAAmB;CAC1B,OAAO,mBAAmB;AAC5B;AACA,SAAS,2BAA2B,MAAM,YAAY,CAAC,GAAG;CACxD,IAAI,KAAK,SAAS,KAAK,MAAM,OAAO,GAAG;EACrC,UAAU,KAAK,IAAI;CACrB;CACA,IAAI,KAAK,UAAU;EACjB,KAAK,MAAM,SAAS,KAAK,UAAU;GACjC,2BAA2B,OAAO,SAAS;EAC7C;CACF;CACA,OAAO;AACT;AACA,SAAS,cAAc,MAAM;CAC3B,IAAI,OAAO,KAAK,WAAW,QAAQ,SAAS,EAAE,CAAC,CAAC,QAAQ,iBAAiB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