"use strict";Object.defineProperty(exports, "__esModule", {value: true});

var _chunkKBVD3QNTjs = require('./chunk-KBVD3QNT.js');


var _chunkXWQFJONKjs = require('./chunk-XWQFJONK.js');


var _chunkZEUAUHNKjs = require('./chunk-ZEUAUHNK.js');


var _chunkXJ65QJ5Kjs = require('./chunk-XJ65QJ5K.js');


var _chunk5PRZ32KIjs = require('./chunk-5PRZ32KI.js');


var _chunkDTIR4DVOjs = require('./chunk-DTIR4DVO.js');


var _chunkMYGO2IQXjs = require('./chunk-MYGO2IQX.js');

// tmp-babel/messages/web/MessagesList/index.js
var _react = require('react');
var _authentication = require('@baseapp-frontend/authentication');
var _displays = require('@baseapp-frontend/design-system/components/web/displays');
var _material = require('@mui/material');
var _reactrelay = require('react-relay');
var _reactvirtuoso = require('react-virtuoso');
var _jsxruntime = require('react/jsx-runtime');
function _typeof(o) {
  "@babel/helpers - typeof";
  return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o2) {
    return typeof o2;
  } : function(o2) {
    return o2 && "function" == typeof Symbol && o2.constructor === Symbol && o2 !== Symbol.prototype ? "symbol" : typeof o2;
  }, _typeof(o);
}
function ownKeys(e, r) {
  var t = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var o = Object.getOwnPropertySymbols(e);
    r && (o = o.filter(function(r2) {
      return Object.getOwnPropertyDescriptor(e, r2).enumerable;
    })), t.push.apply(t, o);
  }
  return t;
}
function _objectSpread(e) {
  for (var r = 1; r < arguments.length; r++) {
    var t = null != arguments[r] ? arguments[r] : {};
    r % 2 ? ownKeys(Object(t), true).forEach(function(r2) {
      _defineProperty(e, r2, t[r2]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function(r2) {
      Object.defineProperty(e, r2, Object.getOwnPropertyDescriptor(t, r2));
    });
  }
  return e;
}
function _defineProperty(e, r, t) {
  return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: true, configurable: true, writable: true }) : e[r] = t, e;
}
function _toPropertyKey(t) {
  var i = _toPrimitive(t, "string");
  return "symbol" == _typeof(i) ? i : i + "";
}
function _toPrimitive(t, r) {
  if ("object" != _typeof(t) || !t) return t;
  var e = t[Symbol.toPrimitive];
  if (void 0 !== e) {
    var i = e.call(t, r || "default");
    if ("object" != _typeof(i)) return i;
    throw new TypeError("@@toPrimitive must return a primitive value.");
  }
  return ("string" === r ? String : Number)(t);
}
function _slicedToArray(r, e) {
  return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
}
function _nonIterableRest() {
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _unsupportedIterableToArray(r, a) {
  if (r) {
    if ("string" == typeof r) return _arrayLikeToArray(r, a);
    var t = {}.toString.call(r).slice(8, -1);
    return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
  }
}
function _arrayLikeToArray(r, a) {
  (null == a || a > r.length) && (a = r.length);
  for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
  return n;
}
function _iterableToArrayLimit(r, l) {
  var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
  if (null != t) {
    var e, n, i, u, a = [], f = true, o = false;
    try {
      if (i = (t = t.call(r)).next, 0 === l) {
        if (Object(t) !== t) return;
        f = false;
      } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = true) ;
    } catch (r2) {
      o = true, n = r2;
    } finally {
      try {
        if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return;
      } finally {
        if (o) throw n;
      }
    }
    return a;
  }
}
function _arrayWithHoles(r) {
  if (Array.isArray(r)) return r;
}
var MessagesList = function MessagesList2(_ref) {
  var _room$allMessages$tot, _room$allMessages, _room$allMessages3, _room$unreadMessages3;
  var roomRef = _ref.roomRef, VirtuosoProps = _ref.VirtuosoProps, _ref$MessagesGroup = _ref.MessagesGroup, MessagesGroup = _ref$MessagesGroup === void 0 ? _chunkKBVD3QNTjs.MessagesGroup_default : _ref$MessagesGroup, _ref$MessagesGroupPro = _ref.MessagesGroupProps, MessagesGroupProps = _ref$MessagesGroupPro === void 0 ? {} : _ref$MessagesGroupPro;
  var _usePaginationFragmen = _reactrelay.usePaginationFragment.call(void 0, _chunk5PRZ32KIjs.MessagesListFragment, roomRef), room = _usePaginationFragmen.data, loadNext = _usePaginationFragmen.loadNext, isLoadingNext = _usePaginationFragmen.isLoadingNext, hasNext = _usePaginationFragmen.hasNext;
  var totalNumberOfMessages = (_room$allMessages$tot = room === null || room === void 0 || (_room$allMessages = room.allMessages) === null || _room$allMessages === void 0 ? void 0 : _room$allMessages.totalCount) !== null && _room$allMessages$tot !== void 0 ? _room$allMessages$tot : 0;
  var virtuosoRef = _react.useRef.call(void 0, null);
  var allMessages = _react.useMemo.call(void 0, function() {
    var _room$allMessages2;
    return (room === null || room === void 0 || (_room$allMessages2 = room.allMessages) === null || _room$allMessages2 === void 0 ? void 0 : _room$allMessages2.edges.filter(function(edge) {
      return edge === null || edge === void 0 ? void 0 : edge.node;
    }).map(function(edge) {
      return edge === null || edge === void 0 ? void 0 : edge.node;
    })) || [];
  }, [room === null || room === void 0 || (_room$allMessages3 = room.allMessages) === null || _room$allMessages3 === void 0 ? void 0 : _room$allMessages3.edges]);
  var allMessagesLastIndex = Number(allMessages === null || allMessages === void 0 ? void 0 : allMessages.length) - 1;
  var firstItemIndex = Math.max(totalNumberOfMessages - allMessages.length, 0);
  var _useCurrentProfile = _authentication.useCurrentProfile.call(void 0, ), currentProfile = _useCurrentProfile.currentProfile;
  var _useChatRoom = _chunkDTIR4DVOjs.useChatRoom_default.call(void 0, ), selectedRoom = _useChatRoom.id;
  var _useReadMessageMutati = _chunkXJ65QJ5Kjs.useReadMessageMutation.call(void 0, ), _useReadMessageMutati2 = _slicedToArray(_useReadMessageMutati, 1), commitMutation = _useReadMessageMutati2[0];
  var getFirstUnreadMessageId = function getFirstUnreadMessageId2() {
    var _room$unreadMessages, _userMessages$find;
    if (!(room !== null && room !== void 0 && (_room$unreadMessages = room.unreadMessages) !== null && _room$unreadMessages !== void 0 && _room$unreadMessages.count)) return null;
    var userMessages = allMessages.filter(function(message) {
      return (message === null || message === void 0 ? void 0 : message.messageType) === _chunkMYGO2IQXjs.MESSAGE_TYPE.user;
    });
    return (_userMessages$find = userMessages.find(function(message, index) {
      var _message$profile;
      var previousMessage = userMessages === null || userMessages === void 0 ? void 0 : userMessages[index + 1];
      var hasPreviousMessage = !!previousMessage;
      return (message === null || message === void 0 || (_message$profile = message.profile) === null || _message$profile === void 0 ? void 0 : _message$profile.id) !== (currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id) && !(message !== null && message !== void 0 && message.isRead) && (!hasPreviousMessage || (previousMessage === null || previousMessage === void 0 ? void 0 : previousMessage.isRead));
    })) === null || _userMessages$find === void 0 ? void 0 : _userMessages$find.id;
  };
  var firstUnreadMessageId = _react.useMemo.call(void 0, getFirstUnreadMessageId, [selectedRoom, currentProfile]);
  _react.useEffect.call(void 0, function() {
    var _room$unreadMessages2;
    if ((room === null || room === void 0 || (_room$unreadMessages2 = room.unreadMessages) === null || _room$unreadMessages2 === void 0 ? void 0 : _room$unreadMessages2.count) !== 0 || room !== null && room !== void 0 && room.unreadMessages.markedUnread) {
      commitMutation({
        variables: {
          input: {
            roomId: room === null || room === void 0 ? void 0 : room.id,
            profileId: currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id
          }
        }
      });
    }
  }, [room === null || room === void 0 ? void 0 : room.id, room === null || room === void 0 || (_room$unreadMessages3 = room.unreadMessages) === null || _room$unreadMessages3 === void 0 ? void 0 : _room$unreadMessages3.count, currentProfile]);
  _chunkZEUAUHNKjs.useMessagesListSubscription.call(void 0, room.id, currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id);
  var renderLoadingState = function renderLoadingState2() {
    if (!isLoadingNext) return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Box, {
      sx: {
        height: 50
      }
    });
    return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _displays.LoadingState, {
      sx: {
        height: 50
      },
      CircularProgressProps: {
        size: 15
      },
      "aria-label": "loading more messages"
    });
  };
  var renderHeader = function renderHeader2() {
    if (allMessages.length === 0) return null;
    return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", {
      className: "h-2"
    });
  };
  var renderMessagesGroup = _react.useCallback.call(void 0, function(index) {
    var messageIndex = allMessages.length - 1 - (index - firstItemIndex);
    var message = allMessages[messageIndex];
    return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, MessagesGroup, _objectSpread({
      allMessages,
      message,
      messageIndex,
      isGroup: room === null || room === void 0 ? void 0 : room.isGroup,
      allMessagesLastIndex,
      firstUnreadMessageId,
      hasNext
    }, MessagesGroupProps));
  }, [allMessages, allMessagesLastIndex, firstItemIndex, hasNext, room === null || room === void 0 ? void 0 : room.isGroup, firstUnreadMessageId, MessagesGroup, MessagesGroupProps]);
  return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _reactvirtuoso.Virtuoso, _objectSpread({
    ref: virtuosoRef,
    firstItemIndex,
    style: {
      overflowX: "hidden"
    },
    initialTopMostItemIndex: allMessagesLastIndex,
    data: allMessages,
    itemContent: renderMessagesGroup,
    startReached: function startReached() {
      if (hasNext) {
        loadNext(_chunkXWQFJONKjs.MESSAGES_TO_LOAD_NEXT);
      }
    },
    components: {
      Header: renderLoadingState,
      Footer: renderHeader
    },
    increaseViewportBy: {
      top: 100,
      bottom: 0
    },
    followOutput: "smooth"
  }, VirtuosoProps));
};
var MessagesList_default = MessagesList;



exports.MessagesList_default = MessagesList_default;
//# sourceMappingURL=chunk-P4FHXATL.js.map