import React from 'react';
import { useToasts } from 'react-toast-notifications';
import { useAllSubscriptions, useGlobalLabels, useErrors } from '@sentecacommerce-theme/lib';
import { Subscription } from './Components';
import { NavigationSkeleton } from '../NavigationSkeleton';
import { Spinner, FormError } from '../../../Common';
export var Subscriptions = () => {
  var {
    errorMessages,
    toastNotifications
  } = useGlobalLabels();
  var {
    addToast
  } = useToasts();

  var 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 = () => {
    var _toastNotifications$s2;

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

  var {
    isAllFetching,
    allSubscriptions,
    checkSubscription,
    handleClick: _handleClick,
    subscribeError,
    unsubscribeError
  } = useAllSubscriptions({
    onSubscribeCallback,
    onUnsubscribeCallback,
    tag: 'userprofile'
  });
  var apiErrors = useErrors(subscribeError || unsubscribeError, {
    default: errorMessages == null ? void 0 : errorMessages.somethingWentWrong
  });
  return /*#__PURE__*/React.createElement(NavigationSkeleton, {
    url: "subscriptions",
    header: "Subscriptions"
  }, isAllFetching || !allSubscriptions ? /*#__PURE__*/React.createElement(Spinner, null) : Object.keys(allSubscriptions).map(item => {
    return /*#__PURE__*/React.createElement(Subscription, {
      key: item,
      header: allSubscriptions[item].label,
      description: allSubscriptions[item].description,
      isChecked: checkSubscription(item),
      handleClick: () => _handleClick(item)
    });
  }), /*#__PURE__*/React.createElement(FormError, {
    errors: apiErrors,
    fieldName: "default"
  }));
};
//# sourceMappingURL=index.js.map