import {
select as e,
put as l,
getContext as i,
call as t,
delay as o,
spawn as n,
race as d,
take as a,
} from "redux-saga/effects"
import {
getMyAudioMuted as r,
getMyVideoMuted as c,
callToggleVideo as s,
callAudioStreamToggle as u,
callLeave as y,
getCallSettingsState as p,
callCloseVideo as f,
} from "../call"
import { uiExpoDeviceVibrateSuccess as g, uiCallFetchAudioOutputs as m } from "./ui.action"
import { APP_CONTEXT_KEY as C, ENV_CONTEXT_KEY as S } from "../constants"
import { setUIAppState as T } from "./ui.reducer"
import { UI_APP_STATE as b } from "./ui.constants"
export function* watchBackgroundVideoToggle() {
let i = !1
for (;;) {
const { appState: t, callEnded: o } = yield d({ appState: a(T), callEnded: a(y) })
if (o) return
const n = t.payload
if (n === b.BACKGROUND) {
const { isVideoMuted: t } = yield e(p)
t || ((i = !0), yield l(f()))
continue
}
if (n === b.ACTIVE && i) {
i = !1
const { isVideoMuted: t } = yield e(p)
t && (yield l(s()))
}
}
}
export function* setupWebrtcCallInfra({
roomTitle: d,
hasExistingCallKit: a,
answeringInAppCallRing: y,
}) {
const { webRTC: p, nativePushModule: f, callBackgroundHandler: T, localeManager: b } = yield i(C),
{ isIOS: I, isAndroid: M } = yield i(S),
w = b.getStrings(),
x = yield e(c)
;((a && !M) ||
(yield t(p.setInCall, {
enabled: !0,
title: w.call.foregroundService,
body: I ? d : w.call.tapToCall,
uri: "keet://screen/room/call",
})),
yield t(T.startService),
yield t(f.stopIncomingCallNotificationService),
I && (yield n(watchBackgroundVideoToggle)),
yield t(p.setKeepScreenOn, !0),
yield t(p.connectToIncomingCall),
yield l(m()),
yield l(g()),
a && !x && (yield l(s())),
yield o(1500))
const V = yield e(r)
;(yield t(p.setCurrentCallMuted, V),
(y || a) && (M && V && (yield l(u())), yield t(p.setCurrentCallMuted, !1)))
}
export function* uiSetWebrtcStatsHandler({ payload: e }) {
const { webRTC: l } = yield i(C)
let t = null
;(e && (t = () => null), l.setDebugHandler(t))
}