"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.roomHeapSlice = exports.removeMessageFromHeapById = exports.markMessageFailed = exports.default = exports.clearMessageFailure = exports.clearHeap = exports.addMessageToHeap = void 0;
var _toolkit = require("@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];
  }
};
const roomHeapSlice = exports.roomHeapSlice = (0, _toolkit.createSlice)({
  name: 'roomHeapStore',
  initialState,
  reducers
});
const {
  addMessageToHeap,
  clearHeap,
  removeMessageFromHeapById,
  markMessageFailed,
  clearMessageFailure
} = roomHeapSlice.actions;
exports.clearMessageFailure = clearMessageFailure;
exports.markMessageFailed = markMessageFailed;
exports.removeMessageFromHeapById = removeMessageFromHeapById;
exports.clearHeap = clearHeap;
exports.addMessageToHeap = addMessageToHeap;
var _default = exports.default = roomHeapSlice.reducer;
//# sourceMappingURL=roomHeapSlice.js.map