"use strict";

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

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

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

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

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

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

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

var _styledComponents = _interopRequireDefault(require("styled-components"));

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

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

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

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

var _Element = require("./Element");

var _templateObject;

var AddressBook = function AddressBook() {
  var _addressesApi$useGet = _lib.addressesApi.useGet({
    enabled: true
  }),
      data = _addressesApi$useGet.data,
      isFetching = _addressesApi$useGet.isFetching;

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

  var _addressesApi$useUpda = _lib.addressesApi.useUpdate(function () {
    return addToast('Successfully updated address', {
      appearance: 'success',
      autoDismiss: true
    });
  }),
      update = _addressesApi$useUpda[0];

  var _addressesApi$useRemo = _lib.addressesApi.useRemove(function () {
    return addToast('Successfully deleted address', {
      appearance: 'success',
      autoDismiss: true
    });
  }),
      remove = _addressesApi$useRemo[0];

  var _useConfigState = (0, _lib.useConfigState)(),
      countryCode = _useConfigState.countryCode;

  var isEmpty = (data == null ? void 0 : data.length) === 0;
  var labels = (0, _lib.useLocalLabels)();
  return /*#__PURE__*/_react["default"].createElement(_NavigationSkeleton.NavigationSkeleton, {
    url: "address-book",
    header: "Address book"
  }, isFetching ? /*#__PURE__*/_react["default"].createElement(_Common.Spinner, null) : /*#__PURE__*/_react["default"].createElement(Container, null, isEmpty && /*#__PURE__*/_react["default"].createElement("p", null, labels.addresses), data == null ? void 0 : data.map(function (address, index) {
    return /*#__PURE__*/_react["default"].createElement(_Element.Element, {
      key: "Address__" + (address._id || index),
      setStatus: /*#__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 update({
                  id: address._id,
                  dto: (0, _extends2["default"])({}, address, {
                    isDefaultShippingAddress: address.isDefaultShippingAddress ? false : true,
                    countryCode: countryCode
                  })
                });

              case 2:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      })),
      handleRemove: /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
        return _regenerator["default"].wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return remove({
                  id: address._id
                });

              case 2:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      })),
      data: address
    });
  }), labels.addressNote));
};

exports.AddressBook = AddressBook;

var Container = _styledComponents["default"].div(_templateObject || (_templateObject = (0, _taggedTemplateLiteralLoose2["default"])(["\n  display: flex;\n  flex-direction: column;\n"])));
//# sourceMappingURL=index.js.map