import {
takeLatest as e,
setContext as n,
getContext as t,
put as o,
cancelled as a,
} from "redux-saga/effects"
import { ENV_CONTEXT_KEY as i } from "../../constants"
import { setTimeStatsSendMsg as s, clearTimeStatsSendMsg as d } from "../stats.reducer"
import {
chatReset as r,
getChatMessageId as l,
sendChatMessageSubmit as c,
sendChatMessageSuccess as m,
setChatMessageCount as y,
setChatMessagesInView as f,
} from "../../chat"
import {
TIME_CONTEXT_KEY as g,
takeStep as u,
sequence as p,
printFlow as h,
createTimeContext as S,
} from "./flow.model"
import { sharedCoreMessagesUpdate as x } from "../../api/data.action"
const M = (e) => (n) => x.match(n) && n.payload.chatMessages[0]?.seq === e,
k = (e, n) => (t) => f.match(t) && (t.payload.bottom === e || t.payload.bottomId === n)
export function* statsSendMessageSaga(e) {
yield n({ [g]: S(Date.now()) })
const t = yield u(m),
o = yield u(y),
a = o.action.payload - 1
o.info = `next seq=${a}`
const i = yield u(M(a)),
s = l(i.action.payload.chatMessages[0])
i.info = `next messageId=${s}`
const d = yield u(k(a, s))
return ((d.info = "rendered"), p(e.name, [t, o, i, d]))
}
export function* statsSendMessage(n) {
const { timeStats: l } = yield t(i),
{ log: m } = l || {},
y = "send message"
;(m(y),
yield e([c, r], function* (e) {
try {
if (r.match(e)) return (yield o(d()), void n.unlockRunning())
if (n.running) return
n.lockRunning()
const t = yield statsSendMessageSaga({ name: y }),
a = yield h(t)
;(n.unlockRunning(), yield o(s(a)))
} finally {
;(yield a()) && n.unlockRunning()
}
}))
}