"use strict";

import { createSlice } from '@reduxjs/toolkit';

// Saved alongside a failed-message id so the retry button can resend
// without the bubble needing to remember the original draft itself.
// `kind === 'text'` carries the plain body; `kind === 'media'` carries
// the upload payload so we don't have to make the user re-pick the file.

const initialState = {
  messageHeap: [],
  failedMessages: {}
};
const reducers = {
  addMessageToHeap: (state, action) => {
    state.messageHeap.push(action.payload);
  },
  removeMessageFromHeapById: (state, action) => {
    const index = state.messageHeap.findIndex(m => m.id === action.payload);
    if (index !== -1) {
      state.messageHeap.splice(index, 1);
    }
  },
  clearHeap: state => {
    state.messageHeap = [];
    state.failedMessages = {};
  },
  markMessageFailed: (state, action) => {
    state.failedMessages[action.payload.id] = action.payload;
    // Drop from the pending heap — the bubble looks at both, and
    // showing "sending..." alongside a failure indicator is confusing.
    const index = state.messageHeap.findIndex(m => m.id === action.payload.id);
    if (index !== -1) {
      state.messageHeap.splice(index, 1);
    }
  },
  clearMessageFailure: (state, action) => {
    delete state.failedMessages[action.payload];
  }
};
export const roomHeapSlice = createSlice({
  name: 'roomHeapStore',
  initialState,
  reducers
});
export const {
  addMessageToHeap,
  clearHeap,
  removeMessageFromHeapById,
  markMessageFailed,
  clearMessageFailure
} = roomHeapSlice.actions;
export default roomHeapSlice.reducer;
//# sourceMappingURL=roomHeapSlice.js.map