import { getContext as a, takeEvery as n } from "redux-saga/effects"
import { APP_CONTEXT_KEY as t } from "../constants"
import { extractRouteNames as e, trimAfterIndex as i } from "./navigation.model"
import { uiNavigateToRoute as o } from "./ui.action"
export function* shouldReplaceNavigation(n, e) {
const { navigationConstants: i } = yield a(t),
{
SCREEN_ROOM: o,
SCREEN_GROUP_CHAT: R,
SCREEN_SCAN_USER_PROFILE: r,
SCREEN_DM_REQUESTS: E,
SCREEN_ROOM_CALL: s,
SCREEN_ROOM_CALL_PREVIEW: _,
SCREEN_USER_PROFILE_FROM_SEARCH: O,
} = i
return (
{
[`${R}->${o}`]: !0,
[`${r}->${o}`]: !0,
[`${E}->${o}`]: !0,
[`${O}->${o}`]: !0,
[`${_}->${s}`]: !0,
}[`${n}->${e}`] || !1
)
}
export function* shouldResetNavigation(n, o) {
const { navigationConstants: R, navigationHelpers: r } = yield a(t),
E = r.navigationRef.getCurrentRoute()?.name
if (n === R.SCREEN_ROOM) {
const a = r.navigationRef.getState(),
n = e(a),
t = n.findIndex((a) => a.name.includes(R.SCREEN_ROOM))
if (E === R.SCREEN_ROOM_DM_REQUEST)
return [
n.find((a) => a.name === R.APP_ROOT) ?? { name: R.APP_ROOT },
{ name: R.SCREEN_ROOM, params: o },
]
if (-1 !== t) {
const a = i(n, t - 1)
return (a.push({ name: R.SCREEN_ROOM, params: o }), a)
}
}
return null
}
export function* navigateSaga(n) {
const { navigationHelpers: e } = yield a(t),
{ routeName: i, params: o } = n.payload,
R = e.navigationRef.getCurrentRoute()?.name
if (yield* shouldReplaceNavigation(R, i)) return void e.navReplace(i, o)
const r = yield* shouldResetNavigation(i, o)
r ? e.resetStackWithHistory(r) : e.navigate(i, o)
}
export function* watchNavigation() {
yield n(o, navigateSaga)
}