import {
  useChatRoom_default
} from "./chunk-U2XGYM5M.mjs";
import {
  getChatRoomConnections
} from "./chunk-2RPURWVV.mjs";
import {
  __esm,
  __export,
  __toCommonJS
} from "./chunk-NFEGQTCC.mjs";

// __generated__/useRoomListSubscription.graphql.ts
var useRoomListSubscription_graphql_exports = {};
__export(useRoomListSubscription_graphql_exports, {
  default: () => useRoomListSubscription_graphql_default
});
var node, useRoomListSubscription_graphql_default;
var init_useRoomListSubscription_graphql = __esm({
  "__generated__/useRoomListSubscription.graphql.ts"() {
    "use strict";
    node = /* @__PURE__ */ function() {
      var v0 = {
        "defaultValue": null,
        "kind": "LocalArgument",
        "name": "connections"
      }, v1 = {
        "defaultValue": null,
        "kind": "LocalArgument",
        "name": "profileId"
      }, v2 = [
        {
          "kind": "Variable",
          "name": "profileId",
          "variableName": "profileId"
        }
      ], v3 = {
        "alias": null,
        "args": null,
        "kind": "ScalarField",
        "name": "id",
        "storageKey": null
      }, v4 = {
        "alias": null,
        "args": null,
        "kind": "ScalarField",
        "name": "isArchived",
        "storageKey": null
      }, v5 = {
        "alias": null,
        "args": null,
        "kind": "ScalarField",
        "name": "participantsCount",
        "storageKey": null
      }, v6 = {
        "alias": null,
        "args": null,
        "kind": "ScalarField",
        "name": "title",
        "storageKey": null
      }, v7 = {
        "alias": null,
        "args": null,
        "concreteType": "Profile",
        "kind": "LinkedField",
        "name": "profile",
        "plural": false,
        "selections": [
          v3
        ],
        "storageKey": null
      }, v8 = [
        {
          "alias": null,
          "args": null,
          "kind": "ScalarField",
          "name": "url",
          "storageKey": null
        }
      ];
      return {
        "fragment": {
          "argumentDefinitions": [
            v0,
            v1
          ],
          "kind": "Fragment",
          "metadata": null,
          "name": "useRoomListSubscription",
          "selections": [
            {
              "alias": null,
              "args": v2,
              "concreteType": "ChatRoomOnRoomUpdate",
              "kind": "LinkedField",
              "name": "chatRoomOnRoomUpdate",
              "plural": false,
              "selections": [
                {
                  "alias": null,
                  "args": null,
                  "concreteType": "ChatRoomEdge",
                  "kind": "LinkedField",
                  "name": "room",
                  "plural": false,
                  "selections": [
                    {
                      "alias": null,
                      "args": null,
                      "concreteType": "ChatRoom",
                      "kind": "LinkedField",
                      "name": "node",
                      "plural": false,
                      "selections": [
                        v3,
                        v4,
                        v5,
                        v6,
                        {
                          "args": null,
                          "kind": "FragmentSpread",
                          "name": "LastMessageFragment"
                        },
                        {
                          "args": null,
                          "kind": "FragmentSpread",
                          "name": "TitleFragment"
                        },
                        {
                          "args": null,
                          "kind": "FragmentSpread",
                          "name": "UnreadMessagesCountFragment"
                        }
                      ],
                      "storageKey": null
                    }
                  ],
                  "storageKey": null
                },
                {
                  "alias": null,
                  "args": null,
                  "concreteType": "ChatRoomParticipant",
                  "kind": "LinkedField",
                  "name": "removedParticipants",
                  "plural": true,
                  "selections": [
                    v3,
                    v7
                  ],
                  "storageKey": null
                }
              ],
              "storageKey": null
            }
          ],
          "type": "Subscription",
          "abstractKey": null
        },
        "kind": "Request",
        "operation": {
          "argumentDefinitions": [
            v1,
            v0
          ],
          "kind": "Operation",
          "name": "useRoomListSubscription",
          "selections": [
            {
              "alias": null,
              "args": v2,
              "concreteType": "ChatRoomOnRoomUpdate",
              "kind": "LinkedField",
              "name": "chatRoomOnRoomUpdate",
              "plural": false,
              "selections": [
                {
                  "alias": null,
                  "args": null,
                  "concreteType": "ChatRoomEdge",
                  "kind": "LinkedField",
                  "name": "room",
                  "plural": false,
                  "selections": [
                    {
                      "alias": null,
                      "args": null,
                      "concreteType": "ChatRoom",
                      "kind": "LinkedField",
                      "name": "node",
                      "plural": false,
                      "selections": [
                        v3,
                        v4,
                        v5,
                        v6,
                        {
                          "alias": null,
                          "args": null,
                          "kind": "ScalarField",
                          "name": "lastMessageTime",
                          "storageKey": null
                        },
                        {
                          "alias": null,
                          "args": null,
                          "concreteType": "Message",
                          "kind": "LinkedField",
                          "name": "lastMessage",
                          "plural": false,
                          "selections": [
                            v3,
                            {
                              "alias": null,
                              "args": null,
                              "kind": "ScalarField",
                              "name": "content",
                              "storageKey": null
                            }
                          ],
                          "storageKey": null
                        },
                        {
                          "alias": null,
                          "args": null,
                          "kind": "ScalarField",
                          "name": "isGroup",
                          "storageKey": null
                        },
                        {
                          "alias": null,
                          "args": [
                            {
                              "kind": "Literal",
                              "name": "first",
                              "value": 2
                            }
                          ],
                          "concreteType": "ChatRoomParticipantConnection",
                          "kind": "LinkedField",
                          "name": "participants",
                          "plural": false,
                          "selections": [
                            {
                              "alias": null,
                              "args": null,
                              "concreteType": "ChatRoomParticipantEdge",
                              "kind": "LinkedField",
                              "name": "edges",
                              "plural": true,
                              "selections": [
                                {
                                  "alias": null,
                                  "args": null,
                                  "concreteType": "ChatRoomParticipant",
                                  "kind": "LinkedField",
                                  "name": "node",
                                  "plural": false,
                                  "selections": [
                                    {
                                      "alias": null,
                                      "args": null,
                                      "concreteType": "Profile",
                                      "kind": "LinkedField",
                                      "name": "profile",
                                      "plural": false,
                                      "selections": [
                                        v3,
                                        {
                                          "alias": null,
                                          "args": null,
                                          "kind": "ScalarField",
                                          "name": "name",
                                          "storageKey": null
                                        },
                                        {
                                          "alias": null,
                                          "args": [
                                            {
                                              "kind": "Literal",
                                              "name": "height",
                                              "value": 100
                                            },
                                            {
                                              "kind": "Literal",
                                              "name": "width",
                                              "value": 100
                                            }
                                          ],
                                          "concreteType": "File",
                                          "kind": "LinkedField",
                                          "name": "image",
                                          "plural": false,
                                          "selections": v8,
                                          "storageKey": "image(height:100,width:100)"
                                        }
                                      ],
                                      "storageKey": null
                                    },
                                    {
                                      "alias": null,
                                      "args": null,
                                      "kind": "ScalarField",
                                      "name": "role",
                                      "storageKey": null
                                    },
                                    v3
                                  ],
                                  "storageKey": null
                                }
                              ],
                              "storageKey": null
                            }
                          ],
                          "storageKey": "participants(first:2)"
                        },
                        {
                          "alias": null,
                          "args": [
                            {
                              "kind": "Literal",
                              "name": "height",
                              "value": 144
                            },
                            {
                              "kind": "Literal",
                              "name": "width",
                              "value": 144
                            }
                          ],
                          "concreteType": "File",
                          "kind": "LinkedField",
                          "name": "image",
                          "plural": false,
                          "selections": v8,
                          "storageKey": "image(height:144,width:144)"
                        },
                        {
                          "alias": null,
                          "args": null,
                          "concreteType": "UnreadMessageCount",
                          "kind": "LinkedField",
                          "name": "unreadMessages",
                          "plural": false,
                          "selections": [
                            {
                              "alias": null,
                              "args": null,
                              "kind": "ScalarField",
                              "name": "count",
                              "storageKey": null
                            },
                            {
                              "alias": null,
                              "args": null,
                              "kind": "ScalarField",
                              "name": "markedUnread",
                              "storageKey": null
                            },
                            v3
                          ],
                          "storageKey": null
                        }
                      ],
                      "storageKey": null
                    }
                  ],
                  "storageKey": null
                },
                {
                  "alias": null,
                  "args": null,
                  "concreteType": "ChatRoomParticipant",
                  "kind": "LinkedField",
                  "name": "removedParticipants",
                  "plural": true,
                  "selections": [
                    v3,
                    {
                      "alias": null,
                      "args": null,
                      "filters": null,
                      "handle": "deleteEdge",
                      "key": "",
                      "kind": "ScalarHandle",
                      "name": "id",
                      "handleArgs": [
                        {
                          "kind": "Variable",
                          "name": "connections",
                          "variableName": "connections"
                        }
                      ]
                    },
                    v7
                  ],
                  "storageKey": null
                }
              ],
              "storageKey": null
            }
          ]
        },
        "params": {
          "cacheID": "a2260a231117b56bb0af9b5197dd5198",
          "id": null,
          "metadata": {},
          "name": "useRoomListSubscription",
          "operationKind": "subscription",
          "text": "subscription useRoomListSubscription(\n  $profileId: ID!\n) {\n  chatRoomOnRoomUpdate(profileId: $profileId) {\n    room {\n      node {\n        id\n        isArchived\n        participantsCount\n        title\n        ...LastMessageFragment\n        ...TitleFragment\n        ...UnreadMessagesCountFragment\n      }\n    }\n    removedParticipants {\n      id\n      profile {\n        id\n      }\n    }\n  }\n}\n\nfragment GroupTitleFragment on ChatRoom {\n  id\n  image(width: 144, height: 144) {\n    url\n  }\n  title\n}\n\nfragment LastMessageFragment on ChatRoom {\n  id\n  lastMessageTime\n  lastMessage {\n    id\n    content\n  }\n}\n\nfragment RoomTitleFragment on ChatRoom {\n  id\n  participants(first: 2) {\n    edges {\n      node {\n        profile {\n          id\n          name\n          image(width: 100, height: 100) {\n            url\n          }\n        }\n        role\n        id\n      }\n    }\n  }\n}\n\nfragment TitleFragment on ChatRoom {\n  id\n  isGroup\n  ...RoomTitleFragment\n  ...GroupTitleFragment\n}\n\nfragment UnreadMessagesCountFragment on ChatRoom {\n  id\n  unreadMessages {\n    count\n    markedUnread\n    id\n  }\n}\n"
        }
      };
    }();
    node.hash = "1a6290e220a68b34641d01ea3ab017a1";
    useRoomListSubscription_graphql_default = node;
  }
});

// tmp-babel/messages/common/graphql/subscriptions/useRoomListSubscription.js
import { useMemo } from "react";
import { ConnectionHandler, graphql, useSubscription } from "react-relay";
var _useRoomListSubscription;
var RoomListSubscriptionQuery = _useRoomListSubscription !== void 0 ? _useRoomListSubscription : (_useRoomListSubscription = (init_useRoomListSubscription_graphql(), __toCommonJS(useRoomListSubscription_graphql_exports)), _useRoomListSubscription.hash && _useRoomListSubscription.hash !== "1a6290e220a68b34641d01ea3ab017a1" && console.error("The definition of 'useRoomListSubscription' appears to have changed. Run `relay-compiler` to update the generated files to receive the expected data."), _useRoomListSubscription);
var wasRemovedFromChatRoom = function wasRemovedFromChatRoom2(data, profileId) {
  var _data$chatRoomOnRoomU;
  return data === null || data === void 0 || (_data$chatRoomOnRoomU = data.chatRoomOnRoomUpdate) === null || _data$chatRoomOnRoomU === void 0 || (_data$chatRoomOnRoomU = _data$chatRoomOnRoomU.removedParticipants) === null || _data$chatRoomOnRoomU === void 0 ? void 0 : _data$chatRoomOnRoomU.some(function(node2) {
    var _node$profile;
    return (node2 === null || node2 === void 0 || (_node$profile = node2.profile) === null || _node$profile === void 0 ? void 0 : _node$profile.id) === profileId;
  });
};
var getRoomListSubscriptionConfig = function getRoomListSubscriptionConfig2(profileId, connections, selectedRoom, resetChatRoom, onRemoval) {
  return {
    subscription: RoomListSubscriptionQuery,
    onError: console.error,
    variables: {
      profileId,
      connections
    },
    updater: function updater(store, data) {
      var _data$chatRoomOnRoomU2;
      var roomId = data === null || data === void 0 || (_data$chatRoomOnRoomU2 = data.chatRoomOnRoomUpdate) === null || _data$chatRoomOnRoomU2 === void 0 || (_data$chatRoomOnRoomU2 = _data$chatRoomOnRoomU2.room) === null || _data$chatRoomOnRoomU2 === void 0 || (_data$chatRoomOnRoomU2 = _data$chatRoomOnRoomU2.node) === null || _data$chatRoomOnRoomU2 === void 0 ? void 0 : _data$chatRoomOnRoomU2.id;
      if (!roomId) return;
      if (wasRemovedFromChatRoom(data, profileId)) {
        getChatRoomConnections(store, profileId).forEach(function(connectionRecord) {
          return ConnectionHandler.deleteNode(connectionRecord, roomId);
        });
      } else {
        var _data$chatRoomOnRoomU3;
        var isArchived = data === null || data === void 0 || (_data$chatRoomOnRoomU3 = data.chatRoomOnRoomUpdate) === null || _data$chatRoomOnRoomU3 === void 0 || (_data$chatRoomOnRoomU3 = _data$chatRoomOnRoomU3.room) === null || _data$chatRoomOnRoomU3 === void 0 || (_data$chatRoomOnRoomU3 = _data$chatRoomOnRoomU3.node) === null || _data$chatRoomOnRoomU3 === void 0 ? void 0 : _data$chatRoomOnRoomU3.isArchived;
        getChatRoomConnections(store, profileId, function(_ref) {
          var q = _ref.q, archived = _ref.archived;
          return q === "" && archived === isArchived;
        }).forEach(function(connectionRecord) {
          var _store$getRootField;
          ConnectionHandler.deleteNode(connectionRecord, roomId);
          var serverEdge = (_store$getRootField = store.getRootField("chatRoomOnRoomUpdate")) === null || _store$getRootField === void 0 ? void 0 : _store$getRootField.getLinkedRecord("room");
          var edge = ConnectionHandler.buildConnectionEdge(store, connectionRecord, serverEdge);
          if (edge) {
            ConnectionHandler.insertEdgeBefore(connectionRecord, edge);
          }
        });
      }
    },
    onNext: function onNext(data) {
      if (wasRemovedFromChatRoom(data, profileId)) {
        var _data$chatRoomOnRoomU4;
        if (selectedRoom && (data === null || data === void 0 || (_data$chatRoomOnRoomU4 = data.chatRoomOnRoomUpdate) === null || _data$chatRoomOnRoomU4 === void 0 || (_data$chatRoomOnRoomU4 = _data$chatRoomOnRoomU4.room) === null || _data$chatRoomOnRoomU4 === void 0 || (_data$chatRoomOnRoomU4 = _data$chatRoomOnRoomU4.node) === null || _data$chatRoomOnRoomU4 === void 0 ? void 0 : _data$chatRoomOnRoomU4.id) === selectedRoom) {
          resetChatRoom();
        }
        onRemoval === null || onRemoval === void 0 || onRemoval();
      }
    }
  };
};
var useRoomListSubscription = function useRoomListSubscription2(_ref2) {
  var connections = _ref2.connections, profileId = _ref2.profileId, onRemoval = _ref2.onRemoval;
  var _useChatRoom = useChatRoom_default(), selectedRoom = _useChatRoom.id, resetChatRoom = _useChatRoom.resetChatRoom;
  var config = useMemo(function() {
    return getRoomListSubscriptionConfig(profileId, connections, selectedRoom, resetChatRoom, onRemoval);
  }, [profileId, connections, onRemoval, selectedRoom, resetChatRoom]);
  return useSubscription(config);
};

export {
  RoomListSubscriptionQuery,
  wasRemovedFromChatRoom,
  getRoomListSubscriptionConfig,
  useRoomListSubscription
};
//# sourceMappingURL=chunk-QSPF5JY5.mjs.map