"use strict";

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

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

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

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

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

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

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

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

var Subscriptions = function Subscriptions() {
  var _useGlobalLabels = (0, _lib.useGlobalLabels)(),
      errorMessages = _useGlobalLabels.errorMessages,
      toastNotifications = _useGlobalLabels.toastNotifications;

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

  var onSubscribeCallback = function onSubscribeCallback() {
    var _toastNotifications$s;

    addToast(toastNotifications == null ? void 0 : (_toastNotifications$s = toastNotifications.success) == null ? void 0 : _toastNotifications$s.subscribe, {
      appearance: 'success',
      autoDismiss: true
    });
  };

  var onUnsubscribeCallback = function onUnsubscribeCallback() {
    var _toastNotifications$s2;

    addToast(toastNotifications == null ? void 0 : (_toastNotifications$s2 = toastNotifications.success) == null ? void 0 : _toastNotifications$s2.unsubscribe, {
      appearance: 'success',
      autoDismiss: true
    });
  };

  var _useAllSubscriptions = (0, _lib.useAllSubscriptions)({
    onSubscribeCallback: onSubscribeCallback,
    onUnsubscribeCallback: onUnsubscribeCallback,
    tag: 'userprofile'
  }),
      isAllFetching = _useAllSubscriptions.isAllFetching,
      allSubscriptions = _useAllSubscriptions.allSubscriptions,
      checkSubscription = _useAllSubscriptions.checkSubscription,
      _handleClick = _useAllSubscriptions.handleClick,
      subscribeError = _useAllSubscriptions.subscribeError,
      unsubscribeError = _useAllSubscriptions.unsubscribeError;

  var apiErrors = (0, _lib.useErrors)(subscribeError || unsubscribeError, {
    "default": errorMessages == null ? void 0 : errorMessages.somethingWentWrong
  });
  return /*#__PURE__*/_react["default"].createElement(_NavigationSkeleton.NavigationSkeleton, {
    url: "subscriptions",
    header: "Subscriptions"
  }, isAllFetching || !allSubscriptions ? /*#__PURE__*/_react["default"].createElement(_Common.Spinner, null) : Object.keys(allSubscriptions).map(function (item) {
    return /*#__PURE__*/_react["default"].createElement(_Components.Subscription, {
      key: item,
      header: allSubscriptions[item].label,
      description: allSubscriptions[item].description,
      isChecked: checkSubscription(item),
      handleClick: function handleClick() {
        return _handleClick(item);
      }
    });
  }), /*#__PURE__*/_react["default"].createElement(_Common.FormError, {
    errors: apiErrors,
    fieldName: "default"
  }));
};

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