import { put as e, select as o, takeLeading as t, fork as i } from "redux-saga/effects"
import { getCoreBackend as r } from "../../../backend"
import { apiCall as d } from "../../../api/core"
import { apiSubscribeSaga as a, forkChildren as l } from "../../lib"
import { taskErrorSaga as s } from "../../error"
import { updateFileEntries as m } from "../../media/file"
import { getMemberItemById as n, parseMemberResponse as c } from "../../member"
import { getCoreId as y, chatMessagesTransform as f, isMessageChanged as p } from "../message"
import {
getChatMessagesInView as g,
setChatMessagesInView as I,
getChatMessageById as h,
} from "../list-view"
import { chatMessageFileTask as u } from "../file"
import { isSeqInRange as M } from "../chat.model"
import {
askReplyingMessageCmd as k,
setReplyingMessage as R,
setReplyToMessage as b,
getChatReplyToMessageById as B,
getChatReplyToInView as T,
getChatReplyPreLoad as S,
getChatReplyToMessageByIdOrNull as x,
} from "./reply-message.reducer"
import { sharedCoreMessagesUpdate as A } from "../../api/data.action"
import {
getChatReactionsById as C,
parseCoreReactions as U,
setChatReactions as v,
shouldUpdateReactions as w,
} from "../reaction"
export function* chatReplyMessageSaga({ roomId: i }) {
yield t(k, function* ({ payload: t }) {
const { messageId: a } = t,
{ memberId: l } = yield o(B, a)
let s = yield o(n(i, l))
if (!s) {
const e = r(),
o = yield d(e.getMember, i, l)
s = c(o)
}
yield e(R({ messageId: a, memberName: s.displayName }))
})
}
export function* fetchReplyToMessageAndUpdate({ roomId: t, ignoreBlocked: i, messageId: a }) {
try {
const l = y(a),
s = r(),
n = yield d(s.getChatMessageById, t, l),
[[c], g] = yield f({ roomId: t, ignoreBlocked: i, coreEvents: [n] })
yield e(m(g))
const I = yield o(x, a)
return ((I && !p(I, c)) || (yield e(b(c))), c)
} catch (e) {
yield s("fetchReplyToMessageAndUpdate", e)
}
return null
}
export function* chatReplyToMessagePreloadSaga({ roomId: t, ignoreBlocked: i }) {
yield l(
[A],
() => o(S),
function* ({ taskId: r }) {
if (yield o(x, r)) return
const d = yield o(h, r)
d
? yield e(b(d))
: yield fetchReplyToMessageAndUpdate({ roomId: t, ignoreBlocked: i, messageId: r })
},
(e) => s("chatReplyToMessagePreloadSaga", e),
)
}
export function* chatReplyToMessageInViewSaga({
roomId: t,
ignoreBlocked: d,
fileStatsChannel: m,
}) {
yield l(
[A, I],
() => o(T),
function* ({ taskId: l }) {
let s = yield o(x, l)
s || (s = yield fetchReplyToMessageAndUpdate({ roomId: t, ignoreBlocked: d, messageId: l }))
const n = yield o(g),
{ seq: c, fileIds: y } = s || {}
y && y.length && !M(n, c) && (yield i(u, { roomId: t, fileIds: y, fileStatsChannel: m }))
const I = r()
yield a([I.subscribeChatMessages, t, { gte: c, lte: c }], function* (i) {
const [r] = i
if (!r) return
const [[a]] = yield f({ roomId: t, ignoreBlocked: d, coreEvents: i }),
s = yield o(x, l)
;(s && !p(s, a)) || (yield e(b(a)))
const m = U(r.reactions, l),
n = yield o(C(l))
w(n, m) && (yield e(v(m)))
})
},
(e) => s("chatReplyToMessageInViewSaga", e),
)
}