import { createAction as e, createSelector as t } from "@reduxjs/toolkit"
import { EMPTY_OBJ as s } from "../../lib/object"
import { myMemberMuted as r } from "../../member"
import { CHAT_STATE_KEY as a } from "../chat.constant"
import { REPLY_MESSAGE_STATE_KEY as o } from "./reply-message.constant"
import { EMPTY_ARRAY as p, arrayEqual as g } from "../../lib/array"
import {
getChatGroupChatMessages as y,
getChatGroupedMessagesInView as l,
getChatMessageState as n,
} from "../list-view"
import { isMessageChanged as i, sendChatMessageSuccess as c } from "../message"
export const askReplyingMessageCmd = e("chat/reply/ask-replying-cmd")
export const setReplyingMessage = e("chat/reply/set-replying-message")
export const clearReplyingMessage = e("chat/reply/clear-replying-message")
export const setReplyToMessage = e("chat/reply/set-message-by-id")
export const REPLY_MESSAGE_INITIAL_STATE = { byId: s, replyingMessage: s }
export const replyMessageReducer = (e, t) => {
switch (t.type) {
case setReplyingMessage.type: {
const { messageId: s, memberName: r } = t.payload || {}
return { ...e, replyingMessage: { messageId: s, memberName: r } }
}
case setReplyToMessage.type: {
const { payload: s } = t,
r = e.byId[s.id]
return r && !i(s, r) ? e : { ...e, byId: { ...e.byId, [s.id]: s } }
}
case r.type:
case c.type:
case clearReplyingMessage.type:
return e.replyingMessage === s ? e : { ...e, replyingMessage: s }
default:
return e
}
}
export const getReplyMessageState = (e) => e[a][o]
export const getReplyingMessage = (e) => getReplyMessageState(e).replyingMessage
export const getReplyingMessageId = (e) => getReplyMessageState(e).replyingMessage?.messageId
export const getChatReplyToMessages = (e) => getReplyMessageState(e).byId
export const getChatReplyToMessageByIdOrNull = (e, t) => getReplyMessageState(e).byId[t] || null
export const getChatReplyToMessageById = (e, t) => getReplyMessageState(e).byId[t] || n(e).byId[t]
export const getChatReplyPreLoad = t(
y,
(e) => {
const t = new Set()
for (let s = 0; s < e.length; s += 1) {
const { replyTo: r } = e[s]
r && !t.has(r) && t.add(r)
}
return t.size ? Array.from(t) : p
},
{ memoizeOptions: { resultEqualityCheck: g } },
)
export const getChatReplyToInView = t(
l,
(e) => {
const t = new Set()
for (let s = 0; s < e.length; s += 1) {
const { replyTo: r } = e[s]
r && !t.has(r) && t.add(r)
}
return t.size ? Array.from(t) : p
},
{ memoizeOptions: { resultEqualityCheck: g } },
)