{"version":3,"sources":["useLinkTo.tsx"],"names":["React","getStateFromPath","getActionFromState","NavigationContext","LinkingContext","useLinkTo","navigation","useContext","linking","linkTo","useCallback","path","startsWith","Error","undefined","options","state","config","root","current","dangerouslyGetParent","action","dispatch","reset"],"mappings":"AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AACA,SACEC,gBADF,EAEEC,kBAFF,EAGEC,iBAHF,QAIO,wBAJP;AAKA,OAAOC,cAAP,MAA2B,kBAA3B;AAEA,eAAe,SAASC,SAAT,GAAqB;AAClC,QAAMC,UAAU,GAAGN,KAAK,CAACO,UAAN,CAAiBJ,iBAAjB,CAAnB;AACA,QAAMK,OAAO,GAAGR,KAAK,CAACO,UAAN,CAAiBH,cAAjB,CAAhB;AAEA,QAAMK,MAAM,GAAGT,KAAK,CAACU,WAAN,CACZC,IAAD,IAAkB;AAChB,QAAI,CAACA,IAAI,CAACC,UAAL,CAAgB,GAAhB,CAAL,EAA2B;AACzB,YAAM,IAAIC,KAAJ,yCAA2CF,IAA3C,QAAN;AACD;;AAED,QAAIL,UAAU,KAAKQ,SAAnB,EAA8B;AAC5B,YAAM,IAAID,KAAJ,CACJ,sFADI,CAAN;AAGD;;AAED,UAAM;AAAEE,MAAAA;AAAF,QAAcP,OAApB;AAEA,UAAMQ,KAAK,GAAG,CAAAD,OAAO,SAAP,IAAAA,OAAO,WAAP,YAAAA,OAAO,CAAEd,gBAAT,IACVc,OAAO,CAACd,gBAAR,CAAyBU,IAAzB,EAA+BI,OAAO,CAACE,MAAvC,CADU,GAEVhB,gBAAgB,CAACU,IAAD,EAAOI,OAAP,aAAOA,OAAP,uBAAOA,OAAO,CAAEE,MAAhB,CAFpB;;AAIA,QAAID,KAAJ,EAAW;AACT,UAAIE,IAAI,GAAGZ,UAAX;AACA,UAAIa,OAAJ,CAFS,CAIT;;AACA,aAAQA,OAAO,GAAGD,IAAI,CAACE,oBAAL,EAAlB,EAAgD;AAC9CF,QAAAA,IAAI,GAAGC,OAAP;AACD;;AAED,YAAME,MAAM,GAAGnB,kBAAkB,CAACc,KAAD,EAAQD,OAAR,aAAQA,OAAR,uBAAQA,OAAO,CAAEE,MAAjB,CAAjC;;AAEA,UAAII,MAAM,KAAKP,SAAf,EAA0B;AACxBI,QAAAA,IAAI,CAACI,QAAL,CAAcD,MAAd;AACD,OAFD,MAEO;AACLH,QAAAA,IAAI,CAACK,KAAL,CAAWP,KAAX;AACD;AACF,KAhBD,MAgBO;AACL,YAAM,IAAIH,KAAJ,CAAU,iDAAV,CAAN;AACD;AACF,GArCY,EAsCb,CAACL,OAAD,EAAUF,UAAV,CAtCa,CAAf;AAyCA,SAAOG,MAAP;AACD","sourcesContent":["import * as React from 'react';\nimport {\n getStateFromPath,\n getActionFromState,\n NavigationContext,\n} from '@react-navigation/core';\nimport LinkingContext from './LinkingContext';\n\nexport default function useLinkTo() {\n const navigation = React.useContext(NavigationContext);\n const linking = React.useContext(LinkingContext);\n\n const linkTo = React.useCallback(\n (path: string) => {\n if (!path.startsWith('/')) {\n throw new Error(`The path must start with '/' (${path}).`);\n }\n\n if (navigation === undefined) {\n throw new Error(\n \"Couldn't find a navigation object. Is your component inside a screen in a navigator?\"\n );\n }\n\n const { options } = linking;\n\n const state = options?.getStateFromPath\n ? options.getStateFromPath(path, options.config)\n : getStateFromPath(path, options?.config);\n\n if (state) {\n let root = navigation;\n let current;\n\n // Traverse up to get the root navigation\n while ((current = root.dangerouslyGetParent())) {\n root = current;\n }\n\n const action = getActionFromState(state, options?.config);\n\n if (action !== undefined) {\n root.dispatch(action);\n } else {\n root.reset(state);\n }\n } else {\n throw new Error('Failed to parse the path to a navigation state.');\n }\n },\n [linking, navigation]\n );\n\n return linkTo;\n}\n"]}