"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.Notifications = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _react = _interopRequireDefault(require("react"));

var _lib = require("@sentecacommerce-theme/lib");

var _reactToastNotifications = require("react-toast-notifications");

var _Components = require("./Components");

var _NavigationSkeleton = require("../NavigationSkeleton");

var _Common = require("../../../Common");

var CustomerMessageStatusUpdateDTOStatusEnum;

(function (CustomerMessageStatusUpdateDTOStatusEnum) {
  CustomerMessageStatusUpdateDTOStatusEnum["Read"] = "read";
  CustomerMessageStatusUpdateDTOStatusEnum["Unread"] = "unread";
})(CustomerMessageStatusUpdateDTOStatusEnum || (CustomerMessageStatusUpdateDTOStatusEnum = {}));

var Notifications = function Notifications() {
  var _data$results, _data$results2;

  var _useToasts = (0, _reactToastNotifications.useToasts)(),
      addToast = _useToasts.addToast;

  var _notificationsApi$use = _lib.notificationsApi.useGet(),
      data = _notificationsApi$use.data,
      isFetching = _notificationsApi$use.isFetching;

  var _notificationsApi$use2 = _lib.notificationsApi.useSetStatus({
    onSuccess: function onSuccess() {
      return addToast(labels.deletedMessage, {
        appearance: 'success',
        autoDismiss: true
      });
    }
  }),
      markAsRead = _notificationsApi$use2[0];

  var filteredResults = data == null ? void 0 : (_data$results = data.results) == null ? void 0 : _data$results.filter(function (message) {
    return message.status !== 'read';
  });
  var isEmpty = (filteredResults == null ? void 0 : filteredResults.length) === 0;
  var labels = (0, _lib.useLocalLabels)();
  return /*#__PURE__*/_react["default"].createElement(_NavigationSkeleton.NavigationSkeleton, {
    url: "notifications",
    header: "Notifications"
  }, isFetching ? /*#__PURE__*/_react["default"].createElement(_Common.Spinner, null) : data == null ? void 0 : (_data$results2 = data.results) == null ? void 0 : _data$results2.map(function (_ref) {
    var message = _ref.message,
        _id = _ref._id,
        status = _ref.status;
    if (status === 'read') return null;
    return /*#__PURE__*/_react["default"].createElement(_Components.Message, {
      key: "Message__" + _id,
      handleRead: /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                _context.next = 2;
                return markAsRead({
                  id: _id,
                  dto: {
                    status: CustomerMessageStatusUpdateDTOStatusEnum.Read
                  }
                });

              case 2:
                return _context.abrupt("return", _context.sent);

              case 3:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      })),
      header: message.header,
      content: message.body
    });
  }), isEmpty && /*#__PURE__*/_react["default"].createElement("p", null, labels.noNotifications));
};

exports.Notifications = Notifications;
//# sourceMappingURL=index.js.map