import {
put as e,
select as i,
takeLeading as t,
getContext as n,
call as o,
take as r,
spawn as c,
} from "redux-saga/effects"
import { getCoreBackend as d } from "../../../backend"
import { apiCall as y } from "../../../api/core"
import {
PREFERENCES as a,
preferenceUpdateTask as l,
getPreferencesIdentityDeviceLinkSeedId as I,
} from "../../preferences"
import { taskErrorSaga as s } from "../../error"
import { PLATFORM_CONTEXT_KEY as E } from "../../constants"
import {
getSyncDeviceSeedId as m,
submitSyncIdentity as f,
setSyncDeviceSeedId as p,
setSyncDeviceErrorMsg as D,
cancelSyncDevice as _,
cancelSyncDeviceSuccess as g,
setIdentity as S,
} from "../identity.reducer"
import { parseInvitation as v } from "../identity.invitation"
export function* syncIdentityInvitationHandler({ payload: i }) {
try {
const {
config: { recognizedAppProtocolList: t },
} = yield n(E),
r = v(i, t)
if (!r) throw new Error("wrong invitation")
const c = d(),
{ seedId: I } = yield y(c.startPairingIdentity, r, { includeSeedId: !0 })
;(yield o(l, a.IDENTITY_DEVICE_LINK_SEED_ID_KEY, I), yield e(p(I)))
} catch (i) {
;(yield e(D(i.message)), yield s("syncIdentityInvitation", i, { logOnly: !0 }))
}
}
export function* cancelSyncDeviceSaga() {
try {
const t = yield i(m)
if (t) {
const i = d()
;(yield y(i.stopPairingRoom, t),
yield o(l, a.IDENTITY_DEVICE_LINK_SEED_ID_KEY, ""),
yield e(g()))
}
} catch (e) {
yield s("cancelSyncDeviceRequestSaga", e)
}
}
export function* syncDeviceSaga() {
;(yield t(_, cancelSyncDeviceSaga), yield t(f, syncIdentityInvitationHandler))
const n = yield i(I)
;(n && (yield e(p(n))), yield r(S), yield c(l, a.IDENTITY_DEVICE_LINK_SEED_ID_KEY, ""))
}