{"version":3,"file":"ngxtension-inject-leaf-activated-route.mjs","sources":["../../../../libs/ngxtension/inject-leaf-activated-route/src/inject-leaf-activated-route.ts","../../../../libs/ngxtension/inject-leaf-activated-route/src/ngxtension-inject-leaf-activated-route.ts"],"sourcesContent":["import { inject } from '@angular/core';\nimport { toSignal } from '@angular/core/rxjs-interop';\nimport { ActivatedRoute, Router } from '@angular/router';\nimport { injectNavigationEnd } from 'ngxtension/navigation-end';\nimport { map } from 'rxjs';\n\n/**\n * Injects the deepest (leaf) activated route as a signal.\n *\n * This function returns a signal that always contains the current leaf route in the router state tree.\n * The leaf route is the deepest child route that has no children of its own. This is useful when you\n * need to access route information from the currently active, deepest route regardless of your\n * component's position in the route hierarchy.\n *\n * The signal updates automatically whenever navigation ends, ensuring it always reflects the current\n * leaf route even when navigating to the same URL (depending on your router configuration's\n * `OnSameUrlNavigation` setting).\n *\n * @returns A signal containing the current leaf ActivatedRoute\n *\n * @example\n * ```ts\n * @Component({\n *   template: `\n *     <div>Current route: {{ leafRoute().snapshot.url }}</div>\n *     <div>Route params: {{ leafRoute().snapshot.params | json }}</div>\n *   `\n * })\n * export class MyComponent {\n *   leafRoute = injectLeafActivatedRoute();\n * }\n * ```\n *\n * @example\n * Access params from the leaf route\n * ```ts\n * @Component({})\n * export class MyComponent {\n *   leafRoute = injectLeafActivatedRoute();\n *   userId = computed(() => this.leafRoute().snapshot.params['id']);\n * }\n * ```\n */\nexport function injectLeafActivatedRoute() {\n\tconst router = inject(Router);\n\tconst navigationEnd$ = injectNavigationEnd();\n\n\treturn toSignal(\n\t\t// Map each navigation end event to the current leaf route\n\t\tnavigationEnd$.pipe(map(() => walkToDeepest(router.routerState.root))),\n\t\t{\n\t\t\t// Set the initial value immediately with the current leaf route\n\t\t\tinitialValue: walkToDeepest(router.routerState.root),\n\t\t\t// Always emit when navigation ends, even if technically navigating to the same route\n\t\t\t// This ensures the signal updates consistently with your router's OnSameUrlNavigation behavior\n\t\t\t// @see [OnSameUrlNavigation](https://angular.dev/api/router/OnSameUrlNavigation)\n\t\t\tequal: () => false,\n\t\t},\n\t);\n}\n\n/**\n * Recursively walks down the ActivatedRoute tree to find the deepest (leaf) child route.\n *\n * The Angular router organizes routes in a tree structure where parent routes can have\n * child routes. This function traverses the tree by following the `firstChild` references\n * until it reaches a route with no children, which is considered the \"leaf\" route.\n *\n * @param step - The current ActivatedRoute node to start traversing from\n * @returns The deepest ActivatedRoute in the tree (the leaf route with no children)\n *\n * @example\n * ```ts\n * // Given route tree: /parent/child/grandchild\n * const root = router.routerState.root;\n * const leaf = walkToDeepest(root);\n * // leaf will be the ActivatedRoute for 'grandchild'\n * ```\n */\nexport function walkToDeepest(step: ActivatedRoute): ActivatedRoute {\n\t// Recursively traverse to the first child until we reach a route with no children\n\treturn step.firstChild ? walkToDeepest(step.firstChild) : step;\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;AAMA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCG;SACa,wBAAwB,GAAA;AACvC,IAAA,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AAC9B,IAAA,MAAM,cAAc,GAAG,mBAAmB,EAAE,CAAC;AAE7C,IAAA,OAAO,QAAQ;;AAEd,IAAA,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,aAAa,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,EACtE;;QAEC,YAAY,EAAE,aAAa,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC;;;;AAIpD,QAAA,KAAK,EAAE,MAAM,KAAK;AAClB,KAAA,CACD,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;AAiBG;AACG,SAAU,aAAa,CAAC,IAAoB,EAAA;;AAEjD,IAAA,OAAO,IAAI,CAAC,UAAU,GAAG,aAAa,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC;AAChE;;AClFA;;AAEG;;;;"}