import {
all as e,
call as o,
fork as i,
put as d,
select as r,
takeLatest as n,
takeEvery as t,
} from "redux-saga/effects"
import { getCoreBackend as l } from "../../../backend"
import { apiCall as m } from "../../../api/core"
import { apiSubscribeSaga as s, apiSubscribeLatest as c } from "../../lib"
import { taskError as a, taskErrorSaga as y } from "../../error"
import { getMemberById as g } from "../../member"
import { getMemberBlockedByIdMap as f, setBlockedMembers as p } from "../../identity/member"
import { updateFileEntries as I } from "../../media/file"
import { parseMessage as u } from "../message"
import { getChatMessageCoreIdById as k } from "../list-view"
import { PINNED_MESSAGES_LIMIT as B } from "../chat.constant"
import {
getPinnedMessageCoreIdById as b,
getIsMessagePinned as h,
getIsPinnedTabOpen as M,
getLastPinnedChatMessage as C,
getPinnedChatMessages as P,
getPinnedChatMessagesCount as q,
} from "./pin-message.selector"
import {
setIsPinnedLimitMessageShown as x,
setPinnedMessages as E,
setPinnedTabIsOpen as H,
updateChatPinnedMessage as S,
toggleMessagePinnedStatusCmd as U,
updateChatPinnedMessageBlocked as v,
} from "./pin-message.action"
import {
getChatReactionsById as w,
parseCoreReactions as R,
setChatReactions as T,
shouldUpdateReactions as j,
} from "../reaction"
function* z({ roomId: e, ignoreBlocked: o }) {
try {
const i = l(),
n = yield m(i.getPinnedChatMessages, e),
t = yield r(g(e)),
s = yield r(f),
c = [],
a = n.map((e) => {
const { message: i, files: d } = u(e, { memberById: t, blocked: !!s[e.memberId] && !o })
return (c.push(...d), i)
})
;(yield d(I(c)), yield d(E(a)))
} catch (e) {
yield y("fetchPinnedMessages", e)
}
}
function* A({ roomId: e, ignoreBlocked: i }) {
const t = l()
yield n(U, function* ({ payload: n }) {
if (!n.messageId) return
const { messageId: l } = n,
s = yield r(q),
c = yield r((e) => h(e, l)),
a = yield r((e) => (c ? b(e, l) : k(e, l)))
if (a)
if (s !== B || c)
try {
const d = c ? t.unpinChatMessage : t.pinChatMessage
;(yield m(d, e, a), yield o(z, { roomId: e, ignoreBlocked: i }))
} catch (e) {
yield y("chatToggleIsPinnedSaga", e)
}
else yield d(x(!0))
})
}
function* D({ roomId: e, ignoreBlocked: o, message: i }) {
yield s(
[l().subscribeChatMessages, e, { gte: i.seq, lte: i.seq }],
function* (n) {
const [t] = n
if (!t) return
const l = yield r(g(e)),
m = !!(yield r(f))[i.memberId] && !o,
{ message: s, files: c } = u(t, { memberById: l, blocked: m }),
{ id: a } = s
;(yield d(I(c)), yield d(S(s)))
const y = R(t.reactions, a),
p = yield r(w(a))
j(p, y) && (yield d(T(y)))
},
{ onError: (e) => d(a("chatPinnedUpdateHandler", e)) },
)
}
function* F({ roomId: e, ignoreBlocked: d }) {
const t = l()
;(yield n(E, function* () {
const o = yield r(C)
o?.coreId && (yield i(D, { roomId: e, ignoreBlocked: d, message: o }))
}),
yield c(
[t.subscribeRoomInfo, e],
function* (i) {
;(yield r(q)) !== i.stats.chat.pinned && (yield o(z, { roomId: e, ignoreBlocked: d }))
},
{ onError: (e) => y("pinMessagesCountUpdateHandler", e) },
))
}
function* G({ roomId: o, ignoreBlocked: d }) {
yield n(H, function* () {
const n = yield r(M),
t = yield r(P),
l = yield r(C)
n &&
t?.length &&
(yield e(
t
.filter((e) => e.id !== l?.id)
.map((e) => i(D, { roomId: o, ignoreBlocked: d, message: e })),
))
})
}
export function* chatPinMessagesSaga({ roomId: e, ignoreBlocked: o }) {
;(yield i(z, { roomId: e, ignoreBlocked: o }),
yield i(F, { roomId: e, ignoreBlocked: o }),
yield i(A, { roomId: e, ignoreBlocked: o }),
yield i(G, { roomId: e, ignoreBlocked: o }),
yield t(p, function* ({ payload: { blockedByMemberId: e } }) {
yield d(v({ blockedByMemberId: e, ignoreBlocked: o }))
}))
}