import {
  UserMessage_default
} from "./chunk-O76KVE42.mjs";
import {
  SystemMessage_default
} from "./chunk-TVCA7WER.mjs";
import {
  DateGroupTypography
} from "./chunk-FX7QYBRF.mjs";
import {
  displayFormattedDate
} from "./chunk-4MPHGCGT.mjs";
import {
  MESSAGE_TYPE
} from "./chunk-3LL3LZ34.mjs";

// tmp-babel/messages/web/MessagesList/MessagesGroup/index.js
import { useCallback } from "react";
import { datesDontHaveSameDay } from "@baseapp-frontend/utils";
import { Box, Divider, Typography, useTheme } from "@mui/material";
import { jsx as _jsx, jsxs as _jsxs } from "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);
}
var MessagesGroup = function MessagesGroup2(_ref) {
  var allMessages = _ref.allMessages, allMessagesLastIndex = _ref.allMessagesLastIndex, hasNext = _ref.hasNext, message = _ref.message, messageIndex = _ref.messageIndex, _ref$isGroup = _ref.isGroup, isGroup = _ref$isGroup === void 0 ? false : _ref$isGroup, firstUnreadMessageId = _ref.firstUnreadMessageId, _ref$SystemMessage = _ref.SystemMessage, SystemMessage = _ref$SystemMessage === void 0 ? SystemMessage_default : _ref$SystemMessage, _ref$SystemMessagePro = _ref.SystemMessageProps, SystemMessageProps = _ref$SystemMessagePro === void 0 ? {} : _ref$SystemMessagePro, _ref$UserMessage = _ref.UserMessage, UserMessage = _ref$UserMessage === void 0 ? UserMessage_default : _ref$UserMessage, _ref$UserMessageProps = _ref.UserMessageProps, UserMessageProps = _ref$UserMessageProps === void 0 ? {} : _ref$UserMessageProps;
  var theme = useTheme();
  var renderDateOnTopOfMessagesGroup = useCallback(function(index) {
    var previousMessage = allMessages === null || allMessages === void 0 ? void 0 : allMessages[index + 1];
    var currentMessage = allMessages === null || allMessages === void 0 ? void 0 : allMessages[index];
    var isLastMessageAvailable = index === allMessagesLastIndex && !hasNext;
    if (isLastMessageAvailable || datesDontHaveSameDay(previousMessage === null || previousMessage === void 0 ? void 0 : previousMessage.created, currentMessage === null || currentMessage === void 0 ? void 0 : currentMessage.created)) {
      return /* @__PURE__ */ _jsx(DateGroupTypography, {
        color: "grey.600",
        variant: "caption",
        children: displayFormattedDate(currentMessage === null || currentMessage === void 0 ? void 0 : currentMessage.created)
      });
    }
    return null;
  }, [allMessages, allMessagesLastIndex, hasNext]);
  var renderUnreadMessagesDivider = useCallback(function(index) {
    var currentMessage = allMessages === null || allMessages === void 0 ? void 0 : allMessages[index];
    if ((currentMessage === null || currentMessage === void 0 ? void 0 : currentMessage.id) === firstUnreadMessageId) {
      return /* @__PURE__ */ _jsx(Divider, {
        variant: "fullWidth",
        sx: {
          "&.MuiDivider-root::before, &.MuiDivider-root::after": {
            borderTop: "1px solid ".concat(theme.palette.error.light)
          }
        },
        children: /* @__PURE__ */ _jsx(Typography, {
          variant: "caption",
          color: "error",
          sx: {
            textAlign: "center"
          },
          children: "New Messages"
        })
      });
    }
    return null;
  }, [allMessages, firstUnreadMessageId, theme.palette.error.light]);
  if (!message) return null;
  return /* @__PURE__ */ _jsxs(Box, {
    display: "flex",
    flexDirection: "column",
    sx: {
      paddingTop: 1 / 2,
      paddingRight: 2
    },
    children: [renderUnreadMessagesDivider(messageIndex), renderDateOnTopOfMessagesGroup(messageIndex), message.messageType === MESSAGE_TYPE.system ? /* @__PURE__ */ _jsx(SystemMessage, _objectSpread({
      messageRef: message
    }, SystemMessageProps)) : /* @__PURE__ */ _jsx(UserMessage, _objectSpread({
      allMessages,
      allMessagesLastIndex,
      message,
      messageIndex,
      isGroup
    }, UserMessageProps))]
  });
};
var MessagesGroup_default = MessagesGroup;

export {
  MessagesGroup_default
};
//# sourceMappingURL=chunk-WYTER6IQ.mjs.map