import { createSlice } from "@reduxjs/toolkit";
let initialState = {
    requestStatus: "not_initiated",
    responseStatus: "recieved",
    haveAnIssue: false,
    issue: "",
    data: []
};
const messageSlice = createSlice({
    name: 'Messages',
    initialState: initialState,
    reducers: {
        initMessages: (state, action) => action.payload,
        setMessages: (state, action) => { state.data = action.payload; },
        addMessage: (state, action) => {
            let existingItem = state.data.findIndex((item) => item.id == action.payload.id);
            if (existingItem == -1) {
                state.data.push(action.payload);
            }
            else {
                state.data[existingItem] = action.payload;
            }
        },
        addMessageChunk: (state, action) => {
            let messageIndex = state.data.findIndex((item) => item.id == action.payload.id);
            if (messageIndex && state.data.length > 0 && state.data[messageIndex].data) {
                state.data[messageIndex].data.content += action.payload.data;
            }
        },
        updateMessage: (state, action) => {
            let messageIndex = state.data.findIndex((item) => item.id == action.payload.id);
            if (messageIndex && state.data.length > 0 && state.data[messageIndex].data) {
                state.data[messageIndex].data = action.payload.data;
            }
        },
        reactMessage: (state, action) => {
            let messageIndex = state.data.findIndex((item) => item.id == action.payload.id);
            if (messageIndex && state.data.length > 0 && state.data[messageIndex]) {
                state.data[messageIndex] = { ...state.data[messageIndex], reaction: action.payload.data };
            }
        },
        removeMessage: (state, action) => {
            state.data = state.data.filter((item) => item.id != action.payload);
        },
        resetMessages: (state, action) => initialState
    }
});
export const { setMessages, initMessages, addMessage, addMessageChunk, removeMessage, resetMessages, reactMessage, updateMessage } = messageSlice.actions;
export default messageSlice.reducer;
//# sourceMappingURL=messageSlice.js.map