{"version":3,"sources":["../../../src/client/core/use-pilot.ts"],"names":["atom","useAtom","Platform","useRouter","Pilot","pilots","DEFAULT_AREA","usePilot","config","areaKey","nextRouter","pilotAtom","safeConfig","pilot"],"mappings":"AAIA,OAAS,QAAAA,EAAY,WAAAC,MAAe,QACpC,OAAS,YAAAC,MAAgB,eACzB,OAAS,aAAAC,MAAiB,cAC1B,OAAS,SAAAC,MAAa,UAKtB,MAAMC,EAEF,CAAC,EAECC,EAAe,YAERC,EAAYC,GAAyC,CACjE,IAAIC,EAAU,OAAOD,GAAW,SAAWA,EAASA,GAAA,YAAAA,EAAQ,GAGxDC,IACHA,EAAUH,GAIX,IAAII,EACAR,EAAS,KAAO,WAAaA,EAAS,KAAO,QAChDQ,EAAaP,EAAU,GAIxB,IAAIQ,EAAYN,EAAOI,GACvB,GAAI,CAACE,EAAW,CACf,MAAMC,GAAc,OAAOJ,GAAW,SAAW,OAAYA,IAAW,CAAC,EACzEG,EAAYX,EACX,IAAII,EAAM,CACT,GAAGQ,EACH,GAAIH,IAAYH,EAAe,OAAYG,EAC3C,WAAYC,CACb,CAAC,CACF,EACAL,EAAOI,GAAWE,CACnB,CAGA,KAAM,CAACE,CAAK,EAAIZ,EAAQU,CAAS,EAGjC,OAAID,GACHG,EAAM,OAAO,CAAE,WAAAH,CAAW,CAAC,EAGrBG,CACR","sourcesContent":["/**\n * © 2022 WavePlay <dev@waveplay.com>\n */\n'use client'\nimport { atom, Atom, useAtom } from 'jotai'\nimport { Platform } from 'react-native'\nimport { useRouter } from 'next/router'\nimport { Pilot } from './pilot'\nimport { PilotConfig } from '../types'\n\n// Fleet of lazy pilots. Or rather, lazy atoms containing pilots.\n// This enables us to create new pilots on demand per area that needs them.\nconst pilots: {\n\t[area: string]: Atom<Pilot>\n} = {}\n\nconst DEFAULT_AREA = '__default'\n\nexport const usePilot = (config?: PilotConfig | string): Pilot => {\n\tlet areaKey = typeof config === 'string' ? config : config?.id\n\n\t// Use default key if none is provided\n\tif (areaKey) {\n\t\tareaKey = DEFAULT_AREA\n\t}\n\n\t// Make sure we always have NextJS router reference on web\n\tlet nextRouter\n\tif (Platform.OS !== 'android' && Platform.OS !== 'ios') {\n\t\tnextRouter = useRouter()\n\t}\n\n\t// Create a new pilot atom if one doesn't exist for this area\n\tlet pilotAtom = pilots[areaKey]\n\tif (!pilotAtom) {\n\t\tconst safeConfig = (typeof config === 'string' ? undefined : config) || {}\n\t\tpilotAtom = atom(\n\t\t\tnew Pilot({\n\t\t\t\t...safeConfig,\n\t\t\t\tid: areaKey === DEFAULT_AREA ? undefined : areaKey,\n\t\t\t\tnextRouter: nextRouter\n\t\t\t})\n\t\t)\n\t\tpilots[areaKey] = pilotAtom\n\t}\n\n\t// Fetch the pilot instance for this area\n\tconst [pilot] = useAtom(pilotAtom)\n\n\t// Make sure the Next.js router instance is always fresh\n\tif (nextRouter) {\n\t\tpilot.config({ nextRouter })\n\t}\n\n\treturn pilot\n}\n"]}