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

export let customRoomMessageQueryMerger: ResponseMergerHandler = function (
  cache,
  request,
  response
) {
  /*if (variables.pivot == null) {
          cache.set(cacheKey, response);
        } else {
          cache.set(cacheKey, cache.get(cacheKey).concat(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.roomId]) {
    cache.getCacheData('messages')[request.variables.roomId] = {};
  }
  response.forEach((msg: MessageRecord) => {
    cache.getCacheData('messages')[request.variables.roomId][msg.id] = msg;
  });
};
