{"version":3,"file":"useNavigation.js","names":["useUpstreamNavigation"],"sources":["esm/router/useNavigation.js"],"sourcesContent":["import {\n  useNavigation as useUpstreamNavigation\n} from \"@react-navigation/native\";\nimport React from \"react\";\nimport { getReactNavigationRouteName } from \"../getReactNavigationConfig\";\nimport { getContextKey, getNameFromFilePath } from \"./matchers\";\nimport { useContextKey, useRouteNodes } from \"./Route\";\nfunction useNavigation(parent) {\n  const navigation = useUpstreamNavigation();\n  const contextKey = useContextKey();\n  const routeNodes = useRouteNodes();\n  const normalizedParent = React.useMemo(() => {\n    if (!parent) {\n      return null;\n    }\n    return resolveParentRouteName(routeNodes, contextKey, parent);\n  }, [contextKey, parent, routeNodes]);\n  if (parent && normalizedParent === void 0) {\n    throw new Error(\n      `Could not find parent navigation with route \"${parent}\". (normalized context: ${resolveParentId(contextKey, parent)})`\n    );\n  }\n  if (normalizedParent != null) {\n    if (normalizedParent === \"\") {\n      let rootNavigation = navigation;\n      let nextNavigation = rootNavigation.getParent();\n      while (nextNavigation) {\n        rootNavigation = nextNavigation;\n        nextNavigation = rootNavigation.getParent();\n      }\n      return rootNavigation;\n    }\n    const parentNavigation = navigation.getParent(normalizedParent);\n    if (!parentNavigation) {\n      throw new Error(\n        `Could not find parent navigation with route \"${parent}\".` + (normalizedParent !== parent ? ` (normalized: ${normalizedParent})` : \"\")\n      );\n    }\n    return parentNavigation;\n  }\n  return navigation;\n}\nfunction resolveParentId(contextKey, parentId) {\n  if (!parentId) {\n    return null;\n  }\n  if (parentId.startsWith(\".\")) {\n    return normalizeContextPath(relativePaths(contextKey, parentId));\n  }\n  return normalizeContextPath(parentId);\n}\nfunction resolveParentRouteName(routeNodes, contextKey, parentId) {\n  const parentContextKey = resolveParentId(contextKey, parentId);\n  if (parentContextKey == null) return null;\n  if (parentContextKey === \"/\") return \"\";\n  const parentRoute = routeNodes.find(\n    (node) => getContextKey(node.contextKey) === parentContextKey\n  );\n  return parentRoute ? getReactNavigationRouteName(parentRoute) : void 0;\n}\nfunction normalizeContextPath(path) {\n  const normalized = getNameFromFilePath(path).replace(/^\\/+|\\/+$/g, \"\").replace(/\\/_layout$/, \"\");\n  return normalized ? `/${normalized}` : \"/\";\n}\nfunction relativePaths(from, to) {\n  const fromParts = from.split(\"/\").filter(Boolean);\n  const toParts = to.split(\"/\").filter(Boolean);\n  for (const part of toParts) {\n    if (part === \"..\") {\n      if (fromParts.length === 0) {\n        throw new Error(`Cannot resolve path \"${to}\" relative to \"${from}\"`);\n      }\n      fromParts.pop();\n    } else if (part === \".\") {\n    } else {\n      fromParts.push(part);\n    }\n  }\n  return \"/\" + fromParts.join(\"/\");\n}\nexport {\n  resolveParentId,\n  resolveParentRouteName,\n  useNavigation\n};\n//# sourceMappingURL=useNavigation.js.map\n"],"mappings":";;;;;;;AAOA,SAAS,cAAc,QAAQ;CAC7B,MAAM,aAAaA,gBAAsB;CACzC,MAAM,aAAa,cAAc;CACjC,MAAM,aAAa,cAAc;CACjC,MAAM,mBAAmB,MAAM,cAAc;EAC3C,IAAI,CAAC,QAAQ;GACX,OAAO;EACT;EACA,OAAO,uBAAuB,YAAY,YAAY,MAAM;CAC9D,GAAG;EAAC;EAAY;EAAQ;CAAU,CAAC;CACnC,IAAI,UAAU,qBAAqB,KAAK,GAAG;EACzC,MAAM,IAAI,MACR,gDAAgD,OAAO,0BAA0B,gBAAgB,YAAY,MAAM,EAAE,EACvH;CACF;CACA,IAAI,oBAAoB,MAAM;EAC5B,IAAI,qBAAqB,IAAI;GAC3B,IAAI,iBAAiB;GACrB,IAAI,iBAAiB,eAAe,UAAU;GAC9C,OAAO,gBAAgB;IACrB,iBAAiB;IACjB,iBAAiB,eAAe,UAAU;GAC5C;GACA,OAAO;EACT;EACA,MAAM,mBAAmB,WAAW,UAAU,gBAAgB;EAC9D,IAAI,CAAC,kBAAkB;GACrB,MAAM,IAAI,MACR,gDAAgD,OAAO,OAAO,qBAAqB,SAAS,iBAAiB,iBAAiB,KAAK,GACrI;EACF;EACA,OAAO;CACT;C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