import {
  MarkAllAsReadButton_default
} from "./chunk-NA77RA5C.mjs";
import {
  NotificationItem_default
} from "./chunk-AQIW7UXR.mjs";
import {
  EmptyState_default
} from "./chunk-PW2SO5WA.mjs";
import {
  HeaderContainer
} from "./chunk-LIGP2EAV.mjs";
import {
  NotificationsListFragment,
  NotificationsListQuery
} from "./chunk-OAQSMDST.mjs";
import {
  useNotificationsSubscription
} from "./chunk-OJZIGPTF.mjs";
import {
  NUMBER_OF_NOTIFICATIONS_TO_LOAD_NEXT
} from "./chunk-3X56BSES.mjs";

// tmp-babel/notifications/web/NotificationsList/index.js
import { Suspense, useMemo } from "react";
import { IconButton } from "@baseapp-frontend/design-system/components/web/buttons";
import { LoadingState as DefaultLoadingState } from "@baseapp-frontend/design-system/components/web/displays";
import { CloseIcon } from "@baseapp-frontend/design-system/components/web/icons";
import { useResponsive } from "@baseapp-frontend/design-system/hooks/web";
import { Box, Divider, Typography } from "@mui/material";
import { useLazyLoadQuery, usePaginationFragment } 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);
}
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 NotificationsList = function NotificationsList2(_ref) {
  var _data$notifications2;
  var setIsDrawerOpened = _ref.setIsDrawerOpened, _ref$EmptyState = _ref.EmptyState, EmptyState = _ref$EmptyState === void 0 ? EmptyState_default : _ref$EmptyState, _ref$LoadingState = _ref.LoadingState, LoadingState = _ref$LoadingState === void 0 ? DefaultLoadingState : _ref$LoadingState, _ref$LoadingStateProp = _ref.LoadingStateProps, LoadingStateProps = _ref$LoadingStateProp === void 0 ? {} : _ref$LoadingStateProp, _ref$NotificationItem = _ref.NotificationItem, NotificationItem = _ref$NotificationItem === void 0 ? NotificationItem_default : _ref$NotificationItem, _ref$NotificationItem2 = _ref.NotificationItemProps, NotificationItemProps = _ref$NotificationItem2 === void 0 ? {} : _ref$NotificationItem2;
  var smDown = useResponsive("down", "sm");
  var options = {
    count: 10
  };
  var _useLazyLoadQuery = useLazyLoadQuery(NotificationsListQuery, options, {
    fetchPolicy: "store-and-network"
  }), me = _useLazyLoadQuery.me;
  var _usePaginationFragmen = usePaginationFragment(NotificationsListFragment, me), data = _usePaginationFragmen.data, loadNext = _usePaginationFragmen.loadNext, isLoadingNext = _usePaginationFragmen.isLoadingNext, hasNext = _usePaginationFragmen.hasNext, refetch = _usePaginationFragmen.refetch;
  useNotificationsSubscription(me === null || me === void 0 ? void 0 : me.id);
  var notifications = useMemo(function() {
    var _data$notifications;
    return (data === null || data === void 0 || (_data$notifications = data.notifications) === null || _data$notifications === void 0 ? void 0 : _data$notifications.edges.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$notifications2 = data.notifications) === null || _data$notifications2 === void 0 ? void 0 : _data$notifications2.edges]);
  var renderNotificationItem = function renderNotificationItem2(notification, index) {
    var _notifications;
    if (!notification) return null;
    if (!notification.unread && (_notifications = notifications[index - 1]) !== null && _notifications !== void 0 && _notifications.unread) {
      return /* @__PURE__ */ _jsxs(_Fragment, {
        children: [/* @__PURE__ */ _jsx(Divider, {
          children: /* @__PURE__ */ _jsx(Typography, {
            variant: "body2",
            children: "Older"
          })
        }), /* @__PURE__ */ _jsx(NotificationItem, _objectSpread({
          notification
        }, NotificationItemProps), "notification-".concat(notification.id))]
      });
    }
    return /* @__PURE__ */ _jsx(NotificationItem, _objectSpread({
      notification
    }, NotificationItemProps), "notification-".concat(notification.id));
  };
  var renderVirtuosoHeader = function renderVirtuosoHeader2() {
    return /* @__PURE__ */ _jsx("div", {
      className: "h-2"
    });
  };
  var renderVirtuosoLoadingState = function renderVirtuosoLoadingState2() {
    if (!isLoadingNext) return /* @__PURE__ */ _jsx(Box, {
      sx: {
        paddingTop: 3
      }
    });
    return /* @__PURE__ */ _jsx(LoadingState, _objectSpread({
      sx: {
        paddingTop: 3
      },
      CircularProgressProps: {
        size: 15
      }
    }, LoadingStateProps));
  };
  var renderContent = function renderContent2() {
    if (notifications.length === 0) return /* @__PURE__ */ _jsx(EmptyState, {});
    return /* @__PURE__ */ _jsx(Box, {
      sx: {
        backgroundColor: "common.white"
      },
      children: /* @__PURE__ */ _jsx(Virtuoso, {
        data: notifications,
        style: {
          height: "calc(100vh - 68px)"
        },
        itemContent: function itemContent(index, notification) {
          return renderNotificationItem(notification, index);
        },
        components: {
          Header: renderVirtuosoHeader,
          Footer: renderVirtuosoLoadingState
        },
        endReached: function endReached() {
          if (hasNext) {
            loadNext(NUMBER_OF_NOTIFICATIONS_TO_LOAD_NEXT);
          }
        }
      })
    });
  };
  var refetchNotifications = function refetchNotifications2() {
    refetch(options, {
      fetchPolicy: "network-only"
    });
  };
  return /* @__PURE__ */ _jsxs(Box, {
    display: "grid",
    gridTemplateRows: "min-content 1fr",
    height: "100%",
    children: [/* @__PURE__ */ _jsxs(HeaderContainer, {
      children: [/* @__PURE__ */ _jsx(Typography, {
        variant: "h6",
        children: "Notifications"
      }), /* @__PURE__ */ _jsxs(Box, {
        display: "grid",
        gridTemplateColumns: "max-content min-content",
        children: [!!(data !== null && data !== void 0 && data.notificationsUnreadCount) && /* @__PURE__ */ _jsx(MarkAllAsReadButton_default, {
          refetch: refetchNotifications
        }), smDown && /* @__PURE__ */ _jsx(IconButton, {
          onClick: function onClick() {
            return setIsDrawerOpened(false);
          },
          "aria-label": "close notifications",
          children: /* @__PURE__ */ _jsx(CloseIcon, {})
        })]
      })]
    }), renderContent()]
  });
};
var NotificationsListSuspended = function NotificationsListSuspended2(props) {
  var _props$LoadingState = props.LoadingState, LoadingState = _props$LoadingState === void 0 ? DefaultLoadingState : _props$LoadingState, _props$LoadingStatePr = props.LoadingStateProps, LoadingStateProps = _props$LoadingStatePr === void 0 ? {} : _props$LoadingStatePr;
  return /* @__PURE__ */ _jsx(Suspense, {
    fallback: /* @__PURE__ */ _jsx(LoadingState, _objectSpread({}, LoadingStateProps)),
    children: /* @__PURE__ */ _jsx(NotificationsList, _objectSpread({}, props))
  });
};
var NotificationsList_default = NotificationsListSuspended;

export {
  NotificationsList_default
};
//# sourceMappingURL=chunk-AXGZ7MLH.mjs.map