import {
  MarkAllAsReadButton_default
} from "./chunk-AG2MALSQ.mjs";
import {
  NotificationItem_default
} from "./chunk-CX4BG4NB.mjs";
import {
  Divider_default
} from "./chunk-PKI2GFYZ.mjs";
import {
  EmptyState_default
} from "./chunk-KJ65J36L.mjs";
import {
  createStyles
} from "./chunk-UOUX2AS3.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/native/NotificationsList/index.js
import React, { Suspense, useMemo } from "react";
import { LoadingScreen } from "@baseapp-frontend/design-system/components/native/displays";
import { Text } from "@baseapp-frontend/design-system/components/native/typographies";
import { InfiniteScrollerView, View } from "@baseapp-frontend/design-system/components/native/views";
import { useTheme } from "@baseapp-frontend/design-system/providers/native";
import { useLazyLoadQuery, usePaginationFragment } from "react-relay";
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 _ref$EmptyState = _ref.EmptyState, EmptyState = _ref$EmptyState === void 0 ? EmptyState_default : _ref$EmptyState, _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 theme = useTheme();
  var styles = createStyles(theme);
  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 refetchNotifications = function refetchNotifications2() {
    refetch(options, {
      fetchPolicy: "store-and-network"
    });
  };
  var renderNotificationItem = function renderNotificationItem2(notification, index) {
    var _notifications;
    if (!notification) return null;
    var divider = null;
    if (!notification.unread && index > 0 && (_notifications = notifications[index - 1]) !== null && _notifications !== void 0 && _notifications.unread) {
      divider = /* @__PURE__ */ _jsx(Divider_default, {});
    }
    return /* @__PURE__ */ _jsxs(_Fragment, {
      children: [divider, /* @__PURE__ */ _jsx(NotificationItem, _objectSpread({
        notification,
        refetch: refetchNotifications
      }, NotificationItemProps))]
    });
  };
  var renderContent = function renderContent2() {
    if (notifications.length === 0) return /* @__PURE__ */ _jsx(EmptyState, {});
    return /* @__PURE__ */ _jsx(View, {
      style: styles.listContainer,
      children: /* @__PURE__ */ _jsx(InfiniteScrollerView, {
        data: notifications,
        renderItem: function renderItem(_ref2) {
          var item = _ref2.item, index = _ref2.index;
          return renderNotificationItem(item, index);
        },
        estimatedItemSize: 134,
        onEndReached: function onEndReached() {
          if (hasNext) {
            loadNext(NUMBER_OF_NOTIFICATIONS_TO_LOAD_NEXT);
          }
        },
        isLoading: isLoadingNext
      })
    });
  };
  return /* @__PURE__ */ _jsxs(View, {
    style: styles.container,
    children: [/* @__PURE__ */ _jsxs(View, {
      style: styles.headerContainer,
      children: [/* @__PURE__ */ _jsx(Text, {
        variant: "h6",
        children: "Notifications"
      }), /* @__PURE__ */ _jsx(MarkAllAsReadButton_default, {
        refetch: refetchNotifications,
        disabled: !(data !== null && data !== void 0 && data.notificationsUnreadCount)
      })]
    }), renderContent()]
  });
};
var SuspendedNotificationsList = function SuspendedNotificationsList2(props) {
  var theme = useTheme();
  var styles = createStyles(theme);
  return /* @__PURE__ */ _jsx(Suspense, {
    fallback: /* @__PURE__ */ _jsxs(View, {
      style: styles.container,
      children: [/* @__PURE__ */ _jsx(View, {
        style: styles.headerContainer,
        children: /* @__PURE__ */ _jsx(Text, {
          variant: "h6",
          children: "Notifications"
        })
      }), /* @__PURE__ */ _jsx(LoadingScreen, {})]
    }),
    children: /* @__PURE__ */ _jsx(NotificationsList, _objectSpread({}, props))
  });
};
var NotificationsList_default = SuspendedNotificationsList;

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