import {
getContext as o,
select as t,
setContext as i,
takeLatest as a,
cancelled as n,
all as e,
} from "redux-saga/effects"
import { ENV_CONTEXT_KEY as r } from "../../constants"
import { setAppCurrentRoomId as m } from "../../app"
import {
VIEW_MODE as d,
getChatMessageState as l,
getChatMessageSeqLoaded as y,
setChatMessagesInView as s,
setChatListViewMode as c,
setChatMessageCount as p,
getChatListViewMode as f,
} from "../../chat"
import { EMPTY_ROOM_ID as u, getRoomItemById as R, setRoomInfo as S } from "../../room"
import {
TIME_CONTEXT_KEY as I,
createTimeContext as T,
printFlow as g,
sequence as h,
parallel as E,
step as A,
stepError as O,
takeStep as b,
} from "./flow.model"
import { sharedCoreMessagesUpdate as x } from "../../api/data.action"
const D = (o) => (t) => m.match(t) && t.payload.roomId === o,
L = (o) => (t) => S.match(t) && t.payload.roomId === o,
_ = (o) => c.match(o) && o.payload === d.RESTORE_LAST,
k = (o) => c.match(o) && o.payload === d.FRESH_LOADING,
w = (o, t) => (i) => {
if (s.match(i)) {
const a = i.payload.top ?? t[i.payload.topId].seq
return (i.payload.bottom ?? t[i.payload.bottomId].seq) - a > 0 || o <= 1
}
return !1
}
export function* statsStartMainRoomSaga(o, { currentTime: a } = {}) {
const n = "start main room"
let r, s
if ((yield i({ [I]: T(a) }), m.match(o))) {
r = o.payload.roomId
const t = a || Date.now()
;(a || (yield i({ [I]: T(t) })), (s = A(o, { startTime: t, doneTime: t })))
} else if ("roomId" in o && o.roomId) ((r = o.roomId), (s = yield b(D(r))))
else {
if (!("nextRoom" in o)) return h(n, [O("[STATS ERROR]: startStartRoomSaga: invalid arguments")])
;((s = yield b(m)), (r = s.action.payload))
}
if (r === u) return ((s.info = "empty room"), s)
const c = Date.now(),
g = yield t(R(r))
let M = A(S(g), { startTime: c, doneTime: c })
g.title || (M = yield b(L(r)))
const $ = yield b(_)
$.info = `mode=${d.RESTORE_LAST}`
const q = yield b(p),
F = yield b(x),
G = yield t(y)
F.info = "loaded=" + (G.bottom - G.top)
const { byId: H, count: N } = yield t(l),
[v, j] = yield e([b(w(N, H)), b(k)])
j.info = `mode=${d.FRESH_LOADING}`
const z = yield b(x),
B = yield t(f)
z.info = `mode=${B}`
const { title: C } = yield t(R(r))
return h(n, [s, M, $, q, F, E([v, j]), z], { title: C })
}
export function* statsStartMainRoom(t) {
const { timeStats: i } = yield o(r),
{ log: e } = i || {}
;(e("start main room"),
yield a(m, function* (o) {
try {
if (t.running) return
t.lockRunning()
const i = yield statsStartMainRoomSaga(o)
;(yield g(i), t.unlockRunning())
} finally {
;(yield n()) && t.unlockRunning()
}
}))
}