import {
call as e,
takeLatest as i,
select as o,
fork as l,
put as a,
getContext as r,
} from "redux-saga/effects"
import { getCoreBackend as t } from "../../backend"
import { apiCall as n } from "../../api/core"
import { PLATFORM_CONTEXT_KEY as c, APP_CONTEXT_KEY as s } from "../constants"
import { taskErrorSaga as f } from "../error"
import {
getProfileWalletConfig as p,
getProfileSoftwareVersion as d,
setProfileCurrentSection as y,
setProfileSoftwareVersion as g,
setProfileSoftwareDependencies as S,
setProfileWalletBalance as m,
setProfileWalletError as u,
setProfileWalletLoading as T,
setProfileWalletTransactions as x,
} from "./ui-user-profile.reducer"
import { UI_PANEL as W, PROFILE_SECTIONS as h } from "./ui.constants"
import { getUIPanelState as w, toggleUIPanel as P } from "./ui.reducer"
async function O(e) {
const i = []
for await (const o of e) i.push(o)
return i
}
export function* profileWalletTransactionsSaga() {
const { lightning: i } = yield r(c)
try {
yield a(T(!0))
const o = yield e([i, i.transactions], { live: !0, seq: 0 }),
l = yield O(o)
yield a(x(l))
} catch (e) {
;(yield a(u(e.message)), yield f("profileWalletTransactionsSaga", e))
} finally {
yield a(T(!1))
}
}
export function* profileWalletBalanceSaga() {
const { lightning: i } = yield r(c)
try {
yield a(m(null))
const o = yield e([i, i.balance])
yield a(m(o))
} catch (e) {
;(yield a(u(e.message)), yield f("profileWalletBalanceSaga", e))
}
}
export function* profileWalletSaga() {
;(yield o(p)) &&
(yield a(u("")), yield l(profileWalletBalanceSaga), yield l(profileWalletTransactionsSaga))
}
export function* profileSoftwareSaga() {
const { versions: e, config: i } = yield r(c)
try {
const { app: o } = yield e()
yield a(g({ appName: i.name, appVersion: o.version }))
} catch (e) {
yield f("profileSoftwareSaga", e)
}
}
export function* profileSoftwareDependenciesSaga() {
try {
if (!(yield o(d)).dependencies) {
const { versions: e } = yield r(s),
i = yield e(),
{ modules: o } = yield n(t().getVersion)
yield a(S({ ...i, ...o }))
}
} catch (e) {
yield f("profileSoftwareDepsSaga", e)
}
}
export function* sectionSetHandler({ payload: e }) {
switch (e) {
case h.WALLET:
yield profileWalletSaga()
break
case h.NETWORK_STATUS:
yield profileSoftwareDependenciesSaga()
break
case h.VERSION:
;(yield profileSoftwareSaga(), yield profileSoftwareDependenciesSaga())
}
}
export function* resetProfileSection() {
const { profile: e } = yield o(w)
e && (yield a(y(h.ROOT)))
}
export function* profileSectionToggle() {
yield resetProfileSection()
}
export const isUIToggleProfilePanel = (e) => P.match(e) && e.payload?.key === W.PROFILE
export function* userProfileSaga() {
;(yield i(y, sectionSetHandler), yield i(isUIToggleProfilePanel, profileSectionToggle))
}