import { MessageRecord } from '../../../../../../server-services/query-records/room-records';
import { ResponseMergerHandler } from '../../../cache-logic-interface';

export let customDialogueMessageQueryMerger: ResponseMergerHandler = function (
  cache,
  request,
  response
) {
  let cacheKey = cache.getCacheLogic().makeCacheDataPath(request);

  let message = cache.getCacheData(cacheKey);
  if (message) {
    cache.setCacheData(cacheKey, message.concat(response));
  } else {
    cache.setCacheData(cacheKey, response);
  }

  // quick access to message
  if (!cache.getCacheData('messages')[request.variables.peerId]) {
    cache.getCacheData('messages')[request.variables.peerId] = {};
  }
  response.forEach((msg: MessageRecord) => {
    cache.getCacheData('messages')[request.variables.peerId][msg.id] = msg;
  });
};
