import {
  Header_default
} from "./chunk-7S62O3QJ.mjs";
import {
  ProfileCard_default
} from "./chunk-ATSU6OG5.mjs";
import {
  Body_default
} from "./chunk-GA5MMND6.mjs";
import {
  LeaveGroupDialog_default
} from "./chunk-FWQIW6VM.mjs";
import {
  GroupDetailsQuery
} from "./chunk-MUWB4VY7.mjs";
import {
  useRoomListSubscription
} from "./chunk-QSPF5JY5.mjs";
import {
  MembersListFragment
} from "./chunk-ADJUUQ5O.mjs";
import {
  useCheckIsAdmin,
  useGroupNameAndAvatar
} from "./chunk-2RPURWVV.mjs";

// tmp-babel/messages/web/GroupChatDetails/index.js
import { Suspense, useRef, useState } from "react";
import { useCurrentProfile } from "@baseapp-frontend/authentication";
import { LoadingState } from "@baseapp-frontend/design-system/components/web/displays";
import { Box } from "@mui/material";
import { ConnectionHandler, usePaginationFragment, usePreloadedQuery } from "react-relay";
import { Virtuoso } from "react-virtuoso";
import { jsx as _jsx, Fragment as _Fragment, 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);
}
var _excluded = ["onBackButtonClicked", "Header", "HeaderProps"];
function _objectWithoutProperties(e, t) {
  if (null == e) return {};
  var o, r, i = _objectWithoutPropertiesLoose(e, t);
  if (Object.getOwnPropertySymbols) {
    var n = Object.getOwnPropertySymbols(e);
    for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]);
  }
  return i;
}
function _objectWithoutPropertiesLoose(r, e) {
  if (null == r) return {};
  var t = {};
  for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
    if (-1 !== e.indexOf(n)) continue;
    t[n] = r[n];
  }
  return t;
}
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 GroupChatDetails = function GroupChatDetails2(_ref) {
  var _currentProfile$id;
  var _ref$Body = _ref.Body, Body = _ref$Body === void 0 ? Body_default : _ref$Body, _ref$BodyProps = _ref.BodyProps, BodyProps = _ref$BodyProps === void 0 ? {} : _ref$BodyProps, _ref$Header = _ref.Header, Header = _ref$Header === void 0 ? Header_default : _ref$Header, _ref$HeaderProps = _ref.HeaderProps, HeaderProps = _ref$HeaderProps === void 0 ? {} : _ref$HeaderProps, onBackButtonClicked = _ref.onBackButtonClicked, onEditButtonClicked = _ref.onEditButtonClicked, _ref$ProfileCard = _ref.ProfileCard, ProfileCard = _ref$ProfileCard === void 0 ? ProfileCard_default : _ref$ProfileCard, _ref$ProfileCardProps = _ref.ProfileCardProps, ProfileCardProps = _ref$ProfileCardProps === void 0 ? {} : _ref$ProfileCardProps, queryRef = _ref.queryRef, _ref$VirtuosoProps = _ref.VirtuosoProps, VirtuosoProps = _ref$VirtuosoProps === void 0 ? {} : _ref$VirtuosoProps;
  var _usePreloadedQuery = usePreloadedQuery(GroupDetailsQuery, queryRef), group = _usePreloadedQuery.chatRoom;
  var _useCurrentProfile = useCurrentProfile(), currentProfile = _useCurrentProfile.currentProfile;
  var _useGroupNameAndAvata = useGroupNameAndAvatar(group), avatar = _useGroupNameAndAvata.avatar, title = _useGroupNameAndAvata.title;
  var profileId = (_currentProfile$id = currentProfile === null || currentProfile === void 0 ? void 0 : currentProfile.id) !== null && _currentProfile$id !== void 0 ? _currentProfile$id : "";
  var connections = group !== null && group !== void 0 && group.id ? [ConnectionHandler.getConnectionID(group.id, "ChatRoom_participants")] : [];
  useRoomListSubscription({
    profileId,
    connections,
    onRemoval: onBackButtonClicked
  });
  var _usePaginationFragmen = usePaginationFragment(MembersListFragment, group), data = _usePaginationFragmen.data, loadNext = _usePaginationFragmen.loadNext, isLoadingNext = _usePaginationFragmen.isLoadingNext, hasNext = _usePaginationFragmen.hasNext;
  var members = data === null || data === void 0 ? void 0 : data.participants;
  var _useCheckIsAdmin = useCheckIsAdmin(members), isAdmin = _useCheckIsAdmin.isAdmin, isSoleAdmin = _useCheckIsAdmin.isSoleAdmin;
  var renderLoadingState = function renderLoadingState2() {
    if (!isLoadingNext) return /* @__PURE__ */ _jsx(Box, {
      sx: {
        paddingTop: 3
      }
    });
    return /* @__PURE__ */ _jsx(LoadingState, {
      sx: {
        paddingTop: 3,
        paddingBottom: 1
      },
      CircularProgressProps: {
        size: 15
      },
      "aria-label": "loading more profiles"
    });
  };
  var _useState = useState(void 0), _useState2 = _slicedToArray(_useState, 2), removingParticipantId = _useState2[0], setRemovingParticipantId = _useState2[1];
  var removingParticipantName = useRef(void 0);
  var initiateRemoval = function initiateRemoval2(id, name) {
    setRemovingParticipantId(id);
    removingParticipantName.current = name;
  };
  var handleRemoveDialogClose = function handleRemoveDialogClose2() {
    setRemovingParticipantId(void 0);
    removingParticipantName.current = void 0;
  };
  var renderItem = function renderItem2(item) {
    if (item.node) {
      return /* @__PURE__ */ _jsx(ProfileCard, _objectSpread({
        groupMember: item.node,
        initiateRemoval,
        hasAdminPermissions: isAdmin
      }, ProfileCardProps));
    }
    return null;
  };
  var renderMembers = function renderMembers2() {
    return /* @__PURE__ */ _jsx(Virtuoso, _objectSpread({
      data: members === null || members === void 0 ? void 0 : members.edges,
      itemContent: function itemContent(_index, item) {
        return renderItem(item);
      },
      style: {
        scrollbarWidth: "none"
      },
      components: {
        Footer: renderLoadingState
      },
      endReached: function endReached() {
        if (hasNext) {
          loadNext(5);
        }
      }
    }, VirtuosoProps));
  };
  return /* @__PURE__ */ _jsxs(_Fragment, {
    children: [/* @__PURE__ */ _jsx(LeaveGroupDialog_default, {
      profileId,
      roomId: group === null || group === void 0 ? void 0 : group.id,
      open: !!removingParticipantId,
      removingParticipantId: removingParticipantId !== null && removingParticipantId !== void 0 ? removingParticipantId : "",
      onClose: handleRemoveDialogClose,
      isSoleAdmin
    }), /* @__PURE__ */ _jsx(Header, _objectSpread({
      onBackButtonClicked,
      onEditButtonClicked,
      shouldDisplayEditButton: isAdmin
    }, HeaderProps)), /* @__PURE__ */ _jsx(Body, _objectSpread(_objectSpread({
      title,
      avatar,
      participantsCount: group === null || group === void 0 ? void 0 : group.participantsCount
    }, BodyProps), {}, {
      children: renderMembers()
    }))]
  });
};
var SuspendedGroupDetails = function SuspendedGroupDetails2(_ref2) {
  var onBackButtonClicked = _ref2.onBackButtonClicked, _ref2$Header = _ref2.Header, Header = _ref2$Header === void 0 ? Header_default : _ref2$Header, _ref2$HeaderProps = _ref2.HeaderProps, HeaderProps = _ref2$HeaderProps === void 0 ? {} : _ref2$HeaderProps, props = _objectWithoutProperties(_ref2, _excluded);
  return (
    // Displays a 'preliminary' header and a spinner below
    // Header has "Group Details" label and back button, but no edit button (appears if the group details are loaded and current user has admin permissions)
    /* @__PURE__ */ _jsx(Suspense, {
      fallback: /* @__PURE__ */ _jsxs(_Fragment, {
        children: [/* @__PURE__ */ _jsx(Header, _objectSpread({
          onBackButtonClicked,
          shouldDisplayEditButton: false
        }, HeaderProps)), /* @__PURE__ */ _jsx(LoadingState, {})]
      }),
      children: /* @__PURE__ */ _jsx(GroupChatDetails, _objectSpread({
        onBackButtonClicked
      }, props))
    })
  );
};
var GroupChatDetails_default = SuspendedGroupDetails;

export {
  GroupChatDetails_default
};
//# sourceMappingURL=chunk-2OAOLWCH.mjs.map