import {
put as t,
call as o,
delay as e,
select as i,
takeLatest as s,
fork as a,
getContext as l,
takeLeading as n,
takeEvery as r,
} from "redux-saga/effects"
import { getCoreBackend as d } from "../../backend"
import { apiCall as m } from "../../api/core"
import {
setStatsData as g,
getStatsEnabled as c,
enableStats as y,
disableStats as p,
toggleStats as f,
unlockStats as u,
unlockClick as S,
getClicksToUnlockToggle as T,
getStatsPollInterval as I,
setTimeStatsData as v,
loadTimeStatsDataCmd as L,
clearTimeStatsAll as k,
} from "./stats.reducer"
import { ENV_CONTEXT_KEY as x, PLATFORM_CONTEXT_KEY as D } from "../constants"
import { taskErrorSaga as E } from "../error"
import {
PREFERENCES as h,
getPreferencesStatsEnabled as w,
getPreferencesTimeStatsEnabled as A,
preferenceUpdateTask as C,
} from "../preferences"
import { TIME_STATS_LOGGER_TYPE as R } from "./flow/flow.model"
import { statsJoinRoom as V } from "./flow/join-room"
import { statsStartMainRoom as $ } from "./flow/start-mainRoom"
import { statsSendMessage as b } from "./flow/chat-sendMesage"
import { statsAppInitialLoad as N } from "./flow/start-app"
export function* statsDeviceInfoSaga() {
const { timeStats: t, appVersion: o, osInfo: e, deviceInfo: i } = yield l(x),
{ groupLogStart: s, groupLogEnd: a, log: n } = t || {}
;(s("[DEVICE]:"), n(`appVersion: ${o}`), n(`osInfo: ${e}`), n(`deviceInfo: ${i}`), a())
}
export function* statsActivitiesSaga() {
const { timeStats: t } = yield l(x),
{ groupLogStart: o, groupLogEnd: e } = t || {},
i = {
running: !1,
lockRunning() {
this.running = !0
},
unlockRunning() {
this.running = !1
},
}
;(o("[STATS INFO] active:"), yield a(V, i), yield a($, i), yield a(b, i), e())
}
export function* pollStatsSaga(o, { statsLogs: s }) {
if (!(yield i(c))) return
const a = d(),
{ getProcessTop: n, getChromiumStats: r } = yield l(D),
y = n ? n()() : null
try {
let o = 0
for (;;) {
const l = yield m(a.getStats),
n = y ? y.cpu() : null,
d = y ? y.memory() : null,
c = r ? r() : null
;(s && y && o % 10 == 0 && console.log(y.toString()),
yield t(g({ data: l, cpu: n, memory: d, chromium: c })))
const p = yield i(I)
;(yield e(p), (o += 1))
}
} catch (t) {
yield E("polling core.getStats", t)
} finally {
y?.destroy()
}
}
export function* toggleStatsSaga() {
const e = yield i(c)
;(yield o(C, h.STATS_ENABLED, !e), e ? yield t(p()) : yield t(y()))
}
export function* unlockClickSaga() {
0 === (yield i(T)) && (yield t(u()))
}
export function* statsSaga({ startTime: o, startDoneTime: e } = {}) {
const d = yield l(x),
{ isDev: m, isStaging: g, statsEnabled: c, timeStats: T, statsLogs: I } = d
if (
((yield i(A)) &&
(T.type !== R.STATS && (yield E("stats saga", "unsupported timeStats type")),
yield a(statsDeviceInfoSaga),
yield a(N, { startTime: o, startDoneTime: e }),
yield a(statsActivitiesSaga),
yield r(L, function* () {
yield t(v(T.getItems()))
}),
yield r(k, T.clearItems)),
yield s([y, p], pollStatsSaga, { statsLogs: I }),
yield n(f, toggleStatsSaga),
yield r(S, unlockClickSaga),
m)
) {
yield t(u())
const o = yield i(w)
;(c || o) && (yield t(y()))
}
g && c && (yield t(y()))
}