import {
fork as e,
put as l,
select as i,
takeLatest as o,
take as d,
all as n,
getContext as t,
} from "redux-saga/effects"
import { isSetCallSettingsReady as r, setCallSettingsReady as a } from "./call-settings.action"
import {
setCallStatus as y,
callJoinCmd as c,
increaseCallPeersCount as m,
openCallSettings as f,
callJoinDMIncoming as s,
callDeclineDMIncoming as u,
callNotificationReceived as p,
callSetIncomingDMCall as C,
callResetIncomingDMCall as I,
decreaseCallPeersCount as h,
callCancel as D,
callVideoStreamToggle as N,
setRoomActiveCall as g,
} from "./call.action"
import {
getCallPeersCount as b,
getCallStatus as x,
isCallJoinedMode as v,
isCallReady as E,
getCallIncomingDMRoomId as A,
getCallIncomingCall as M,
getIsCallsV2Supported as S,
} from "./call.reducer"
import { CALL_STATUS as T, ENABLE_INCOMING_DM_CALL_NOTIFY as P } from "./call.constants"
import { getMemberBlocked as L, getMyMemberId as R } from "../identity"
import {
getRoomMainId as k,
getIsRoomDm as G,
getRoomNotificationById as J,
switchRoomSubmit as _,
getRoomDmMemberId as w,
} from "../room"
import { CHAT_MESSAGE_TYPE as H } from "../chat"
import { getCoreBackend as O } from "../../backend"
import { apiCall as U } from "../../api/core"
import { taskErrorSaga as j } from "../error"
import { getMyVideoMuted as q } from "./call-settings.reducer"
import { getPreferencesNotificationsType as z, NOTIFICATIONS_TYPES as B } from "../preferences"
import { ENV_CONTEXT_KEY as F } from "../constants"
import { getUIAppBackgrounded as K, getUINativeCallRinging as Q } from "../ui-mobile/ui.reducer"
import { uiStartCall as V } from "../ui-mobile/ui.action"
function W() {
return D()
}
function* X(e, l) {
const { isMobile: o } = yield t(F),
d = yield i(M)
if (d?.roomId) return !1
const n = yield i(J(e)),
r = yield i(z),
a = yield i((e) => L(e, l))
if (!n || r === B.NONE || a) return !1
if (o) {
const e = yield i(K),
l = yield i(Q)
if (e || l) return !1
}
return !0
}
function* Y() {
yield o(g, function* ({ payload: e }) {
const { roomId: o, callId: d, memberId: n, isMyCall: t } = e
if (!d || t) {
const e = yield i(M)
return void (e?.roomId === o && (yield l(I())))
}
const r = yield i(x)
v(r) ||
r === T.SETTINGS ||
((yield i(G(o))) && n && (yield X(o, n)) && (yield l(C({ roomId: o, callId: d }))))
})
}
export function* handleDmCallNotifyDecline() {
try {
yield l(I())
} catch (e) {
yield j("handleDmCallNotifyDecline", e)
}
}
export function* handleDmCallNotifyEnd(e) {
const o = yield i(M)
if (o?.roomId === e)
try {
yield l(I())
} catch (e) {
yield j("handleDmCallNotifyEnd", e)
}
}
export function* handleDmCallNotifyIncoming(e) {
const o = yield i(w(e))
;(yield X(e, o)) && ((yield i(x)) === T.SETTINGS && (yield l(W())), yield l(C({ roomId: e })))
}
export function* handleDmCallNotifyJoin({ payload: e }) {
const { roomId: o, videoMuted: n } = e,
{ isMobile: m } = yield t(F),
s = yield i(k),
u = yield i(M)
if ((yield l(I()), s !== u?.roomId))
for (yield l(_({ roomId: o })); ; ) {
yield d(y)
const e = yield i(x)
if (E(e)) break
}
for (yield l(f()); ; ) {
const e = yield d(a)
if (r(e)) break
}
const p = yield i(q)
if ((p !== n && (yield l(N())), m))
return void (yield l(V({ videoMuted: p, answeringInAppCallRing: !0 })))
let C = u
if (!C?.callId && (yield i(S(o)))) {
const e = O(),
l = (yield U(e.getActiveCalls, o)).find((e) => !e.endedAt)
l && (C = { ...C, callId: l.callId })
}
yield l(c(C))
}
export function* handleDmCallNotify({ message: e, roomId: l }) {
const o = yield i(G(l)),
d = yield i(x),
n = v(d)
if (!o || n) return
const {
memberId: t,
event: { type: r },
} = e
;(yield i(R)) !== t &&
(r === H.CALL_STARTED && (yield handleDmCallNotifyIncoming(l)),
r === H.CALL_HANGUP && (yield handleDmCallNotifyEnd(l)))
}
export function* handleCallNotificationReceived({ payload: e }) {
const { message: l, roomId: i } = e || {}
l && i && (yield handleDmCallNotify({ message: l, roomId: i }))
}
export function* handleIncreaseCallPeersCount({ payload: e }) {
const { roomId: o } = e,
{ isMobile: d } = yield t(F),
[r, a, y, c, m] = yield n([i(G(o)), i(A), i(b, o), i(k), i(x)]),
f = v(m)
;(!r || a || o !== c || 1 !== y || f || d || (yield handleDmCallNotifyIncoming(o)),
r && a && r && a && o === a && y > 1 && (yield l(I())))
}
export function* handleDecreaseCallPeersCount({ payload: e }) {
const { roomId: l } = e,
[o, d, t, r, a] = yield n([i(G(l)), i(A), i(b, l), i(k), i(x)]),
y = v(a)
o && d && d === l && l === r && 0 === t && !y && (yield handleDmCallNotifyEnd(l))
}
function* Z() {
;(yield o(m, handleIncreaseCallPeersCount),
yield o(h, handleDecreaseCallPeersCount),
yield o(p, handleCallNotificationReceived))
}
export function* callDMSaga() {
P &&
(yield e(Y),
yield e(Z),
yield o(s, handleDmCallNotifyJoin),
yield o(u, handleDmCallNotifyDecline))
}