import {
  Timestamp_default
} from "./chunk-YH4AI32S.mjs";
import {
  MessageItem_default
} from "./chunk-D6SECXG4.mjs";
import {
  MAXIMUM_DIFF_TO_GROUP_MESSAGES_CREATED_TIME
} from "./chunk-3LL3LZ34.mjs";

// tmp-babel/messages/web/MessagesList/MessagesGroup/UserMessage/index.js
import { useCallback, useMemo } from "react";
import { useCurrentProfile } from "@baseapp-frontend/authentication";
import { AvatarWithPlaceholder } from "@baseapp-frontend/design-system/components/web/avatars";
import { Box, Typography } from "@mui/material";
import { DateTime } from "luxon";
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 UserMessage = function UserMessage2(_ref) {
  var _message$profile, _message$profile2, _message$profile3;
  var allMessages = _ref.allMessages, allMessagesLastIndex = _ref.allMessagesLastIndex, message = _ref.message, messageIndex = _ref.messageIndex, _ref$isGroup = _ref.isGroup, isGroup = _ref$isGroup === void 0 ? false : _ref$isGroup, _ref$MessageItem = _ref.MessageItem, MessageItem = _ref$MessageItem === void 0 ? MessageItem_default : _ref$MessageItem, _ref$MessageItemProps = _ref.MessageItemProps, MessageItemProps = _ref$MessageItemProps === void 0 ? {} : _ref$MessageItemProps;
  var _useCurrentProfile = useCurrentProfile(), currentProfile = _useCurrentProfile.currentProfile;
  var renderLastMessageTime = useCallback(function(index) {
    var _nextMessage$profile, _currentMessage$profi;
    var currentMessage = allMessages === null || allMessages === void 0 ? void 0 : allMessages[index];
    var nextMessage = allMessages === null || allMessages === void 0 ? void 0 : allMessages[index - 1];
    var isNextMessageFromOtherParticipant = (nextMessage === null || nextMessage === void 0 || (_nextMessage$profile = nextMessage.profile) === null || _nextMessage$profile === void 0 ? void 0 : _nextMessage$profile.id) !== (currentMessage === null || currentMessage === void 0 || (_currentMessage$profi = currentMessage.profile) === null || _currentMessage$profi === void 0 ? void 0 : _currentMessage$profi.id);
    var _DateTime$fromISO$dif = DateTime.fromISO(nextMessage === null || nextMessage === void 0 ? void 0 : nextMessage.created).diff(DateTime.fromISO(currentMessage === null || currentMessage === void 0 ? void 0 : currentMessage.created), "minutes"), dateDiff = _DateTime$fromISO$dif.minutes;
    var isDateDiffAboveMaximumInterval = dateDiff > MAXIMUM_DIFF_TO_GROUP_MESSAGES_CREATED_TIME;
    if (isNextMessageFromOtherParticipant || !nextMessage || isDateDiffAboveMaximumInterval) {
      return /* @__PURE__ */ _jsx(Timestamp_default, {
        date: currentMessage === null || currentMessage === void 0 ? void 0 : currentMessage.created
      });
    }
    return null;
  }, [allMessages]);
  var isFirstGroupedMessage = useMemo(function() {
    var _previousMessage$prof, _currentMessage$profi2;
    var currentMessage = allMessages === null || allMessages === void 0 ? void 0 : allMessages[messageIndex];
    var previousMessage = allMessages === null || allMessages === void 0 ? void 0 : allMessages[messageIndex + 1];
    var isPreviousMessageFromOtherParticipant = (previousMessage === null || previousMessage === void 0 || (_previousMessage$prof = previousMessage.profile) === null || _previousMessage$prof === void 0 ? void 0 : _previousMessage$prof.id) !== (currentMessage === null || currentMessage === void 0 || (_currentMessage$profi2 = currentMessage.profile) === null || _currentMessage$profi2 === void 0 ? void 0 : _currentMessage$profi2.id);
    var roomHasOnlyOneMessage = allMessagesLastIndex === 0;
    if (isPreviousMessageFromOtherParticipant || roomHasOnlyOneMessage) return true;
    return false;
  }, [allMessages, allMessagesLastIndex, messageIndex]);
  var isOwnMessage = (currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id) === (message === null || message === void 0 || (_message$profile = message.profile) === null || _message$profile === void 0 ? void 0 : _message$profile.id);
  var flexAlignments = isOwnMessage ? "flex-end" : "flex-start";
  var canShowAvatar = isFirstGroupedMessage && !isOwnMessage;
  var canShowName = canShowAvatar && isGroup;
  if (!message) return null;
  return /* @__PURE__ */ _jsxs(Box, {
    sx: {
      display: "flex",
      flexDirection: "row",
      alignSelf: flexAlignments,
      width: "100%"
    },
    children: [canShowAvatar && /* @__PURE__ */ _jsx(Box, {
      paddingRight: "12px",
      children: /* @__PURE__ */ _jsx(AvatarWithPlaceholder, {
        className: "self-start justify-self-center",
        width: 32,
        height: 32,
        src: message === null || message === void 0 || (_message$profile2 = message.profile) === null || _message$profile2 === void 0 || (_message$profile2 = _message$profile2.image) === null || _message$profile2 === void 0 ? void 0 : _message$profile2.url,
        sx: {
          border: "none"
        }
      })
    }), /* @__PURE__ */ _jsxs(Box, {
      sx: {
        display: "flex",
        ml: isFirstGroupedMessage ? 0 : 5.5,
        alignSelf: flexAlignments,
        alignItems: flexAlignments,
        flexDirection: "column",
        width: "100%"
      },
      children: [canShowName && /* @__PURE__ */ _jsx(Typography, {
        variant: "subtitle2",
        color: "text.primary",
        marginBottom: 1 / 2,
        children: message === null || message === void 0 || (_message$profile3 = message.profile) === null || _message$profile3 === void 0 ? void 0 : _message$profile3.name
      }), /* @__PURE__ */ _jsx(MessageItem, _objectSpread({
        messageRef: message,
        isGroup,
        isFirstGroupedMessage
      }, MessageItemProps)), renderLastMessageTime(messageIndex)]
    })]
  });
};
var UserMessage_default = UserMessage;

export {
  UserMessage_default
};
//# sourceMappingURL=chunk-O76KVE42.mjs.map