import {
  GroupTitleFragment
} from "./chunk-NIOX2TGN.mjs";
import {
  RoomTitleFragment
} from "./chunk-6LKVRW5I.mjs";
import {
  CHAT_ROOM_PARTICIPANT_ROLES
} from "./chunk-3LL3LZ34.mjs";

// tmp-babel/messages/common/utils.js
import { useCurrentProfile } from "@baseapp-frontend/authentication";
import { useFragment } from "react-relay";
import ConnectionHandler from "relay-connection-handler-plus";
var useGroupNameAndAvatar = function useGroupNameAndAvatar2(headerRef) {
  var _header$image;
  var header = useFragment(GroupTitleFragment, headerRef);
  return {
    title: header === null || header === void 0 ? void 0 : header.title,
    avatar: header === null || header === void 0 || (_header$image = header.image) === null || _header$image === void 0 ? void 0 : _header$image.url
  };
};
var useRoomNameAndAvatar = function useRoomNameAndAvatar2(headerRef) {
  var _otherParticipant$nod, _otherParticipant$nod2;
  var _useCurrentProfile = useCurrentProfile(), currentProfile = _useCurrentProfile.currentProfile;
  var header = useFragment(RoomTitleFragment, headerRef);
  if (!(header !== null && header !== void 0 && header.participants)) {
    return {
      title: "Error: No participants"
    };
  }
  var otherParticipant = header.participants.edges.find(function(edge) {
    var _edge$node, _edge$node2;
    return (edge === null || edge === void 0 || (_edge$node = edge.node) === null || _edge$node === void 0 || (_edge$node = _edge$node.profile) === null || _edge$node === void 0 ? void 0 : _edge$node.id) && (edge === null || edge === void 0 || (_edge$node2 = edge.node) === null || _edge$node2 === void 0 || (_edge$node2 = _edge$node2.profile) === null || _edge$node2 === void 0 ? void 0 : _edge$node2.id) !== (currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id);
  });
  return {
    title: otherParticipant === null || otherParticipant === void 0 || (_otherParticipant$nod = otherParticipant.node) === null || _otherParticipant$nod === void 0 || (_otherParticipant$nod = _otherParticipant$nod.profile) === null || _otherParticipant$nod === void 0 ? void 0 : _otherParticipant$nod.name,
    avatar: otherParticipant === null || otherParticipant === void 0 || (_otherParticipant$nod2 = otherParticipant.node) === null || _otherParticipant$nod2 === void 0 || (_otherParticipant$nod2 = _otherParticipant$nod2.profile) === null || _otherParticipant$nod2 === void 0 || (_otherParticipant$nod2 = _otherParticipant$nod2.image) === null || _otherParticipant$nod2 === void 0 ? void 0 : _otherParticipant$nod2.url
  };
};
var useNameAndAvatar = function useNameAndAvatar2(roomHeader) {
  var roomNameAndAvatar = useRoomNameAndAvatar(roomHeader);
  var groupNameAndAvatar = useGroupNameAndAvatar(roomHeader);
  if (roomHeader.isGroup) return groupNameAndAvatar;
  return roomNameAndAvatar;
};
var getParticipantCountString = function getParticipantCountString2(participantCount) {
  if (participantCount !== void 0 && participantCount !== null) {
    return "".concat(participantCount, " member").concat(participantCount !== 1 ? "s" : "");
  }
  return void 0;
};
var getChatRoomConnections = function getChatRoomConnections2(store, profileId, filter) {
  var storyRecord = store.get(profileId);
  if (storyRecord) {
    return ConnectionHandler.getConnections(storyRecord, "roomsList_chatRooms", filter);
  }
  return [];
};
var useCheckIsAdmin = function useCheckIsAdmin2(participants) {
  var _participants$edges, _me$node, _participants$edges2;
  var _useCurrentProfile2 = useCurrentProfile(), currentProfile = _useCurrentProfile2.currentProfile;
  var me = participants === null || participants === void 0 || (_participants$edges = participants.edges) === null || _participants$edges === void 0 ? void 0 : _participants$edges.find(function(edge) {
    var _edge$node3;
    return (edge === null || edge === void 0 || (_edge$node3 = edge.node) === null || _edge$node3 === void 0 || (_edge$node3 = _edge$node3.profile) === null || _edge$node3 === void 0 ? void 0 : _edge$node3.id) === (currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id);
  });
  var isAdmin = (me === null || me === void 0 || (_me$node = me.node) === null || _me$node === void 0 ? void 0 : _me$node.role) === CHAT_ROOM_PARTICIPANT_ROLES.admin;
  var isSoleAdmin = isAdmin && (participants === null || participants === void 0 || (_participants$edges2 = participants.edges) === null || _participants$edges2 === void 0 ? void 0 : _participants$edges2.filter(function(edge) {
    var _edge$node4;
    return (edge === null || edge === void 0 || (_edge$node4 = edge.node) === null || _edge$node4 === void 0 ? void 0 : _edge$node4.role) === CHAT_ROOM_PARTICIPANT_ROLES.admin;
  }).length) === 1;
  return {
    isAdmin,
    isSoleAdmin
  };
};

export {
  useGroupNameAndAvatar,
  useNameAndAvatar,
  getParticipantCountString,
  getChatRoomConnections,
  useCheckIsAdmin
};
//# sourceMappingURL=chunk-2RPURWVV.mjs.map