import {
LOADING_STATUS as e,
appendSortedItems as t,
prependSortedItems as o,
} from "../lib/clock-list"
import { parseMessage as s } from "../chat"
import { REPORTED_MESSAGES_DEFAULT_LIMIT as r } from "./reported-messages.constant"
import {
updateLatestReportedMessagesOrder as a,
appendOlderReportedOrder as n,
upsertReportedMessagesById as c,
updateReportedMessagesLoadingStatus as d,
setReportedMessagesInViewRange as l,
updateSelectedReportedMessage as p,
resetReportedMessages as i,
resetSelectedReportedMessage as I,
} from "./reported-messages.action"
export const INITIAL_STATE = {
sortedItems: [],
byId: {},
fileById: {},
minClock: null,
inView: { top: -1, bottom: -1 },
loadingStatus: e.INITIAL,
selectedItem: null,
}
const m = (e) => {
const { message: t } = s(e)
return { id: t?.id, clock: t?.reportedClock }
}
export const reportedMessagesReducer = (e = INITIAL_STATE, y) => {
switch (y.type) {
case d.type:
return { ...e, loadingStatus: y.payload }
case a.type: {
const t = y.payload.map(m)
return { ...e, ...o(t, e) }
}
case n.type: {
const o = y.payload.map(m),
s = o.length < r
return { ...e, ...t(o, { sortedItems: e.sortedItems, allLoaded: s }) }
}
case c.type: {
const t = {},
o = {}
for (const e of y.payload) {
const { id: r } = m(e),
{ message: a, files: n } = s(e)
if (((t[r] = a), n?.length)) for (const e of n) o[e?.id] = e
}
return { ...e, byId: { ...e.byId, ...t }, fileById: { ...e.fileById, ...o } }
}
case l.type:
return { ...e, inView: y.payload }
case p.type:
return { ...e, selectedItem: y.payload }
case I.type:
return { ...e, selectedItem: null }
case i.type:
return INITIAL_STATE
default:
return e
}
}