import { fork as l, put as o, select as e, join as i } from "redux-saga/effects"
import { getCoreBackend as r } from "../../backend"
import { apiCall as t } from "../../api/core"
import { getMyMemberId as a } from "../identity"
import { taskErrorSaga as d } from "../error"
import {
openCallSettings as m,
callReset as c,
setCallStatus as n,
setCallIds as y,
setPendingCall as f,
} from "./call.action"
import { setCallSettingsSkipJoinMsg as s } from "./call-settings.action"
import { CALL_STATUS as p } from "./call.constants"
import { getIsCallsV2Supported as g } from "./call.reducer"
import { callOngoingSaga as I } from "./call-ongoing.saga"
import { getMyDeviceId as u, isDeviceInCall as b } from "./utils"
export function* callSaga({ roomId: v, startNew: A, skipJoinMsg: R, resume: h }) {
let k = null
try {
;(yield o(f(null)), yield o(n(p.LOADING)))
const x = yield e(a),
D = r(),
K = (yield t(D.getRoomKeys, v)).discoveryKey
if (!K || !x)
return (yield o(c()), void (yield d("connect call", "unable to get room and member data")))
;(yield o(y({ callId: K, memberId: x })),
R && (yield o(s(!0))),
(k = yield l(I, { roomId: v, callId: K })),
yield o(n(p.READY)),
A && (yield o(m())))
const N = yield e(g(v))
if (h && N) {
const l = yield t(D.getActiveCalls, v)
if (l.length > 0) {
const e = yield t(D.getRoomInfo, v)
b(l[0], u(e)) && (yield o(f({ roomId: v, callId: l[0].callId })))
}
}
yield i(k)
} catch (l) {
yield d("callSaga", l)
} finally {
;(k?.cancel(), yield o(c()))
}
}