import _taggedTemplateLiteralLoose from "@babel/runtime/helpers/taggedTemplateLiteralLoose";
import _extends from "@babel/runtime/helpers/extends";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";

var _templateObject;

import React from 'react';
import styled from 'styled-components';
import { useToasts } from 'react-toast-notifications';
import { addressesApi, useConfigState, useLocalLabels } from '@sentecacommerce-theme/lib';
import { NavigationSkeleton } from '../NavigationSkeleton';
import { Spinner } from '../../../Common';
import { Element } from './Element';
export var AddressBook = () => {
  var {
    data,
    isFetching
  } = addressesApi.useGet({
    enabled: true
  });
  var {
    addToast
  } = useToasts();
  var [update] = addressesApi.useUpdate(() => addToast('Successfully updated address', {
    appearance: 'success',
    autoDismiss: true
  }));
  var [remove] = addressesApi.useRemove(() => addToast('Successfully deleted address', {
    appearance: 'success',
    autoDismiss: true
  }));
  var {
    countryCode
  } = useConfigState();
  var isEmpty = (data == null ? void 0 : data.length) === 0;
  var labels = useLocalLabels();
  return /*#__PURE__*/React.createElement(NavigationSkeleton, {
    url: "address-book",
    header: "Address book"
  }, isFetching ? /*#__PURE__*/React.createElement(Spinner, null) : /*#__PURE__*/React.createElement(Container, null, isEmpty && /*#__PURE__*/React.createElement("p", null, labels.addresses), data == null ? void 0 : data.map((address, index) => {
    return /*#__PURE__*/React.createElement(Element, {
      key: "Address__" + (address._id || index),
      setStatus: /*#__PURE__*/_asyncToGenerator(function* () {
        yield update({
          id: address._id,
          dto: _extends({}, address, {
            isDefaultShippingAddress: address.isDefaultShippingAddress ? false : true,
            countryCode: countryCode
          })
        });
      }),
      handleRemove: /*#__PURE__*/_asyncToGenerator(function* () {
        yield remove({
          id: address._id
        });
      }),
      data: address
    });
  }), labels.addressNote));
};
var Container = styled.div(_templateObject || (_templateObject = _taggedTemplateLiteralLoose(["\n  display: flex;\n  flex-direction: column;\n"])));
//# sourceMappingURL=index.js.map