"use strict";Object.defineProperty(exports, "__esModule", {value: true});

var _chunkKRRJ7PJ6js = require('./chunk-KRRJ7PJ6.js');


var _chunkHYAGZSK7js = require('./chunk-HYAGZSK7.js');



var _chunk54VHOTGEjs = require('./chunk-54VHOTGE.js');



var _chunkAZYOKZPQjs = require('./chunk-AZYOKZPQ.js');


var _chunkLK72HAZ6js = require('./chunk-LK72HAZ6.js');


var _chunk33MQOH72js = require('./chunk-33MQOH72.js');


var _chunkDTIR4DVOjs = require('./chunk-DTIR4DVO.js');


var _chunk25CJDQG6js = require('./chunk-25CJDQG6.js');

// tmp-babel/messages/web/AllChatRoomsList/index.js
var _react = require('react');
var _displays = require('@baseapp-frontend/design-system/components/web/displays');
var _images = require('@baseapp-frontend/design-system/components/web/images');
var _inputs = require('@baseapp-frontend/design-system/components/web/inputs');
var _material = require('@mui/material');
var _reacthookform = require('react-hook-form');
var _reactvirtuoso = require('react-virtuoso');
var _jsxruntime = require('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);
}
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 AllChatRoomsList = function AllChatRoomsList2(_ref) {
  var _targetRef$me, _data$chatRooms2;
  var targetRef = _ref.targetRef, _ref$onHeaderClick = _ref.onHeaderClick, onHeaderClick = _ref$onHeaderClick === void 0 ? function() {
  } : _ref$onHeaderClick, _ref$Searchbar = _ref.Searchbar, Searchbar = _ref$Searchbar === void 0 ? _inputs.Searchbar : _ref$Searchbar, _ref$SearchbarProps = _ref.SearchbarProps, SearchbarProps = _ref$SearchbarProps === void 0 ? {} : _ref$SearchbarProps, _ref$ChatRoomItem = _ref.ChatRoomItem, ChatRoomItem = _ref$ChatRoomItem === void 0 ? _chunkKRRJ7PJ6js.ChatRoomItem_default : _ref$ChatRoomItem, _ref$ChatRoomItemProp = _ref.ChatRoomItemProps, ChatRoomItemProps = _ref$ChatRoomItemProp === void 0 ? {} : _ref$ChatRoomItemProp, _ref$EmptyChatRoomsSt = _ref.EmptyChatRoomsState, EmptyChatRoomsState = _ref$EmptyChatRoomsSt === void 0 ? _chunkHYAGZSK7js.EmptyChatRoomsState_default : _ref$EmptyChatRoomsSt, _ref$VirtuosoProps = _ref.VirtuosoProps, VirtuosoProps = _ref$VirtuosoProps === void 0 ? {} : _ref$VirtuosoProps;
  var _useState = _react.useState.call(void 0, _chunk54VHOTGEjs.CHAT_TAB_VALUES.active), _useState2 = _slicedToArray(_useState, 2), tab = _useState2[0], setTab = _useState2[1];
  var _useState3 = _react.useState.call(void 0, true), _useState4 = _slicedToArray(_useState3, 2), renderList = _useState4[0], setRenderList = _useState4[1];
  var _useTransition = _react.useTransition.call(void 0, ), _useTransition2 = _slicedToArray(_useTransition, 2), isRefetchPending = _useTransition2[0], startRefetchTransition = _useTransition2[1];
  var _useRoomsList = _chunk33MQOH72js.useRoomsList.call(void 0, targetRef === null || targetRef === void 0 || (_targetRef$me = targetRef.me) === null || _targetRef$me === void 0 ? void 0 : _targetRef$me.profile), data = _useRoomsList.data, loadNext = _useRoomsList.loadNext, isLoadingNext = _useRoomsList.isLoadingNext, hasNext = _useRoomsList.hasNext, refetch = _useRoomsList.refetch;
  var _useTransition3 = _react.useTransition.call(void 0, ), _useTransition4 = _slicedToArray(_useTransition3, 2), isPending = _useTransition4[0], startTransition = _useTransition4[1];
  var _useForm = _reacthookform.useForm.call(void 0, {
    defaultValues: {
      search: ""
    }
  }), control = _useForm.control, reset = _useForm.reset, watch = _useForm.watch;
  var isInArchivedTab = tab === _chunk54VHOTGEjs.CHAT_TAB_VALUES.archived;
  var isInUnreadTab = tab === _chunk54VHOTGEjs.CHAT_TAB_VALUES.unread;
  var searchValue = watch("search");
  var handleSearchChange = function handleSearchChange2(e) {
    var value = e.target.value || "";
    startTransition(function() {
      refetch({
        q: value,
        unreadMessages: isInUnreadTab,
        archived: isInArchivedTab
      }, {
        fetchPolicy: "network-only"
      });
    });
  };
  var handleSearchClear = function handleSearchClear2() {
    startTransition(function() {
      reset();
      refetch({
        q: "",
        unreadMessages: isInUnreadTab,
        archived: isInArchivedTab
      }, {
        fetchPolicy: "network-only"
      });
    });
  };
  var handleChange = function handleChange2(event, newTab) {
    setTab(newTab);
    startRefetchTransition(function() {
      setRenderList(false);
      refetch({
        q: searchValue,
        unreadMessages: newTab === _chunk54VHOTGEjs.CHAT_TAB_VALUES.unread,
        archived: newTab === _chunk54VHOTGEjs.CHAT_TAB_VALUES.archived
      }, {
        fetchPolicy: "network-only"
      });
    });
  };
  _react.useEffect.call(void 0, function() {
    setRenderList(true);
  }, [data]);
  var _useChatRoom = _chunkDTIR4DVOjs.useChatRoom_default.call(void 0, ), selectedRoom = _useChatRoom.id, setChatRoom = _useChatRoom.setChatRoom;
  var chatRooms = _react.useMemo.call(void 0, function() {
    var _data$chatRooms;
    return (data === null || data === void 0 || (_data$chatRooms = data.chatRooms) === null || _data$chatRooms === void 0 || (_data$chatRooms = _data$chatRooms.edges) === null || _data$chatRooms === void 0 ? void 0 : _data$chatRooms.filter(function(edge) {
      return edge === null || edge === void 0 ? void 0 : edge.node;
    }).map(function(edge) {
      return edge === null || edge === void 0 ? void 0 : edge.node;
    })) || [];
  }, [data === null || data === void 0 || (_data$chatRooms2 = data.chatRooms) === null || _data$chatRooms2 === void 0 ? void 0 : _data$chatRooms2.edges]);
  _chunkLK72HAZ6js.useRoomListSubscription.call(void 0, {
    profileId: data.id,
    connections: []
  });
  var renderItem = _react.useCallback.call(void 0, function(room) {
    if (!room) return null;
    return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, ChatRoomItem, _objectSpread({
      roomRef: room,
      isCardSelected: selectedRoom === room.id,
      handleClick: function handleClick() {
        setChatRoom({
          id: room.id
        });
      },
      isInArchivedTab
    }, ChatRoomItemProps));
  }, [selectedRoom, setChatRoom, ChatRoomItemProps, ChatRoomItem, isInArchivedTab]);
  var renderLoadingState = function renderLoadingState2() {
    if (!isLoadingNext) return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Box, {
      sx: {
        paddingTop: 3
      }
    });
    return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _displays.LoadingState, {
      sx: {
        paddingTop: 3,
        paddingBottom: 1
      },
      CircularProgressProps: {
        size: 15
      },
      "aria-label": "loading more rooms"
    });
  };
  var renderTabLabel = function renderTabLabel2(tabValue) {
    return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.Box, {
      display: "grid",
      gap: 1,
      gridTemplateColumns: "1fr min-content",
      alignItems: "center",
      children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Typography, {
        variant: "subtitle2",
        color: "text.primary",
        children: _chunk54VHOTGEjs.CHAT_TAB_LABEL[tabValue]
      }), isRefetchPending && tab === tabValue && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.CircularProgress, {
        size: 15
      })]
    });
  };
  var renderListContent = function renderListContent2() {
    var hasEmptyStates = !isPending && chatRooms.length === 0;
    if (hasEmptyStates) {
      if (searchValue) return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunk25CJDQG6js.SearchNotFoundState_default, {});
      return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, EmptyChatRoomsState, {});
    }
    return void 0;
  };
  return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, {
    children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkAZYOKZPQjs.Header, {
      children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.Box, {
        display: "grid",
        width: "100%",
        gridTemplateColumns: "auto min-content",
        gap: 1,
        children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Typography, {
          variant: "h4",
          component: "span",
          children: "Messages"
        }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Button, {
          variant: "contained",
          color: "inherit",
          startIcon: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _images.Iconify, {
            icon: "mingcute:add-line"
          }),
          onClick: onHeaderClick,
          children: "New"
        })]
      })
    }), /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkAZYOKZPQjs.MainContainer, {
      children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Box, {
        sx: {
          paddingX: {
            xs: 1.5,
            sm: 2.5
          },
          paddingTop: 2
        },
        children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 
          Searchbar,
          _objectSpread({
            name: "search",
            control,
            onChange: handleSearchChange,
            onClear: handleSearchClear,
            isPending
          }, SearchbarProps),
          tab
          /* The handleSearchChange function depends on tab.
          Searchbar calls useRef on the onChange function (to debounce),
          hence it does not see the changes unless it is reloaded */
        )
      }), /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _material.Tabs, {
        value: tab,
        onChange: handleChange,
        centered: true,
        variant: "fullWidth",
        sx: {
          paddingX: {
            xs: 0,
            sm: 2.5
          },
          paddingTop: 2
        },
        children: [/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Tab, {
          label: renderTabLabel(_chunk54VHOTGEjs.CHAT_TAB_VALUES.active),
          value: _chunk54VHOTGEjs.CHAT_TAB_VALUES.active
        }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Tab, {
          label: renderTabLabel(_chunk54VHOTGEjs.CHAT_TAB_VALUES.unread),
          value: _chunk54VHOTGEjs.CHAT_TAB_VALUES.unread
        }), /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _material.Tab, {
          label: renderTabLabel(_chunk54VHOTGEjs.CHAT_TAB_VALUES.archived),
          value: _chunk54VHOTGEjs.CHAT_TAB_VALUES.archived
        })]
      }), renderListContent(), renderList && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _reactvirtuoso.Virtuoso, _objectSpread({
        data: chatRooms,
        overscan: 1,
        itemContent: function itemContent(_index, item) {
          return renderItem(item);
        },
        style: {
          scrollbarWidth: "none"
        },
        components: {
          Footer: renderLoadingState
        },
        endReached: function endReached() {
          if (hasNext) {
            loadNext(5);
          }
        }
      }, VirtuosoProps))]
    })]
  });
};
var AllChatRoomsList_default = AllChatRoomsList;



exports.AllChatRoomsList_default = AllChatRoomsList_default;
//# sourceMappingURL=chunk-4GYPIEOQ.js.map