import { chatInit as e } from "../init"
import { EMPTY_ACTION as t } from "../../lib/reducer"
import { chatMessageClickEvt as r } from "../message"
import {
chatSearchDisable as n,
chatSearchGoNextMsgCmd as a,
chatSearchGoPrevMsgCmd as d,
chatSearchInputEvt as s,
setChatSearchChatMode as c,
setChatSearchListMode as o,
addChatSearchMessages as i,
setChatSearchTerms as m,
toggleChatSearching as u,
setChatSearchMessagesDone as h,
} from "./chat-search.action"
import {
EMPTY_INDEX as p,
SEARCH_DATA_STATUS as I,
SEARCH_MODE as y,
CHAT_SEARCH_INITIAL_STATE as l,
CHAT_SEARCH_LIMIT as f,
CHAT_SEARCH_LIMIT_CONFIG as g,
CHAT_SEARCH_STATE_KEY as x,
} from "./chat-search.constant"
import {
addSegmentHighlight as T,
createHighlightSegments as S,
isChatSearching as A,
isEmptySearchTerms as E,
isSearchTermsLimited as R,
trimTextAndSegment as v,
} from "./chat-search.model"
export const chatSearchReducer = (C = l, H = t) => {
switch (H.type) {
case n.type:
return C === l ? C : l
case u.type:
return A(C.mode) ? l : { ...C, mode: y.CHAT }
case m.type: {
if (!A(C.mode)) return C
const e = H.payload.filter((e) => !!e),
{ mode: t } = C
if (E(e)) return { ...l, mode: t }
if (R(e)) return { ...l, searchTerms: e, mode: t }
const r = e.length ? I.LOADING_MORE : I.RESET
return { ...l, mode: t, searchTerms: e, dataStatus: r }
}
case i.type: {
const e = H.payload,
{ ids: t, byId: r, mode: n, searchTerms: a } = C
if (!A(n)) return C
if (0 === e.length) return C
const {
nextIds: d,
nextById: s,
changedIds: c,
} = e.reduce(
(e, t) => {
const r = S(a, t.chat.text),
n = T(t.chat.segments, r),
d = v(t.chat.text, n, C.config),
s = { ...t, chat: { ...t.chat, ...d, segments: n } }
return (
(e.nextById[t.id] = s),
e.nextIds.includes(t.id) || (e.nextIds.push(t.id), (e.changedIds = !0)),
e
)
},
{ nextIds: [...t], nextById: { ...r }, changedIds: !1 },
)
return { ...C, ids: c ? d : t, byId: s, dataStatus: I.READY }
}
case h.type:
return C.dataStatus === I.READY ? C : { ...C, dataStatus: I.READY }
case r.type: {
if (!A(C.mode)) return C
const { messageId: e } = H.payload,
t = C.ids.findIndex((t) => t === e)
return t === p || (t === C.activeIndex && C.mode === y.CHAT)
? C
: { ...C, activeIndex: t, mode: y.CHAT }
}
case d.type: {
const { activeIndex: e, ids: t } = C
if (!A(C.mode) || !t.length) return C
let r = e
return (
(r = e === p || 0 === e ? t.length - 1 : Math.min(e - 1, t.length - 1)),
r === e ? C : { ...C, activeIndex: r }
)
}
case a.type: {
const { activeIndex: e, ids: t } = C
if (!A(C.mode) || !t.length) return C
let r = e
return (
(r = e === p || e === t.length - 1 ? 0 : Math.min(e + 1, t.length - 1)),
r === e ? C : { ...C, activeIndex: r }
)
}
case s.type: {
const e = H.payload.trim().length <= f.CHARS
if (!A(C.mode) || e) return C
const t = C.searchTerms.length ? I.LOADING_MORE : I.RESET
return C.dataStatus === t ? C : { ...C, dataStatus: t }
}
case o.type:
return C.mode !== y.CHAT ? C : { ...C, mode: y.LIST }
case c.type:
return C.mode !== y.LIST ? C : { ...C, mode: y.CHAT }
case e.type: {
const e = H.payload?.[x]
return e ? { ...C, config: { ...g, ...e } } : C
}
default:
return C
}
}