"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

var _Form = _interopRequireDefault(require("../Modules/Form"));

var _AddressBox = _interopRequireDefault(require("../Modules/AddressBox"));

var _ArraySelector = _interopRequireDefault(require("../Modules/ArraySelector"));

var _AddressInput = _interopRequireDefault(require("../Inputs/AddressInput"));

var _Toast = _interopRequireDefault(require("../helpers/Toast"));

var _Merge = _interopRequireDefault(require("../helpers/Merge"));

var _DictionaryService = _interopRequireDefault(require("../Services/DictionaryService"));

var _reactMaterialize = require("react-materialize");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

var Addresses =
/*#__PURE__*/
function (_React$Component) {
  _inherits(Addresses, _React$Component);

  function Addresses(props) {
    var _this;

    _classCallCheck(this, Addresses);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(Addresses).call(this, props));
    _this.state = {
      addresses: [],
      showAddressForm: false,
      selectedAddress: {},
      edit: false
    };

    _this.updateService(props.service);

    return _this;
  }

  _createClass(Addresses, [{
    key: "componentWillReceiveProps",
    value: function componentWillReceiveProps(props) {
      if (props.service) {
        var service = props.service;
        this.updateService(service);
      }
    }
  }, {
    key: "updateService",
    value: function updateService(service) {
      var _this2 = this;

      service.getPage(1, 100).then(function (addresses) {
        _this2.setState({
          addresses: addresses,
          showAddressForm: false
        });
      });
    }
  }, {
    key: "edit",
    value: function edit(address) {
      this.setState({
        showAddressForm: true,
        edit: true,
        editAddress: address
      });
    }
  }, {
    key: "delete",
    value: function _delete(address) {
      var _this3 = this;

      this.props.update && this.props.update('' + Math.random() * Math.random());
      if (window.confirm(_DictionaryService.default.get("Delete address ?"))) this.props.service.update(address.id, {
        deleted: true
      }).then(function () {
        return _this3.updateService(_this3.props.service);
      });
    }
  }, {
    key: "getAddressForm",
    value: function getAddressForm() {
      var _this4 = this;

      return _react.default.createElement("div", null, _react.default.createElement(_Form.default, {
        FormInputs: this.state.edit ? (0, _Merge.default)(_AddressInput.default, this.state.editAddress) : _AddressInput.default,
        formData: function formData(address) {
          return _this4.setState({
            newAddress: address
          });
        }
      }), _react.default.createElement(_reactMaterialize.Button, {
        onClick: function onClick() {
          var action = _this4.state.edit ? _this4.props.service.update(_this4.state.editAddress.id, _this4.state.newAddress) : _this4.props.service.add(_this4.state.newAddress);
          action.then(function () {
            return _this4.updateService(_this4.props.service);
          }).then(function () {
            _this4.props.update && _this4.props.update(Math.random());

            _this4.setState({
              showAddressForm: false
            });
          });
        }
      }, this.state.edit ? _DictionaryService.default.get('Update') : _DictionaryService.default.get('Add')), !!this.state.addresses.length && _react.default.createElement(_reactMaterialize.Button, {
        onClick: function onClick() {
          _this4.setState({
            showAddressForm: false
          });
        }
      }, _DictionaryService.default.get('Cancel')));
    }
  }, {
    key: "getSelector",
    value: function getSelector() {
      var _this5 = this;

      return _react.default.createElement(_react.default.Fragment, null, _react.default.createElement(_ArraySelector.default, {
        Array: this.state.addresses,
        displayItem: function displayItem(address, indx) {
          return _react.default.createElement(_AddressBox.default, {
            key: indx,
            Address: address,
            Selected: _this5.state.selectedAddress.id == address.id,
            onSelect: function onSelect() {
              _this5.setState({
                selectedAddress: address
              });

              _this5.props.onSelect && _this5.props.onSelect(address);
            },
            onEdit: function onEdit() {
              return _this5.edit(address);
            },
            onDelete: function onDelete() {
              return _this5.delete(address);
            }
          });
        }
      }), _react.default.createElement(_reactMaterialize.Button, {
        onClick: function onClick() {
          return _this5.setState({
            showAddressForm: true,
            edit: false
          });
        }
      }, _DictionaryService.default.get("Add address")));
    }
  }, {
    key: "render",
    value: function render() {
      if (!this.state.showAddressForm && !this.state.addresses.length) {
        this.setState({
          showAddressForm: true
        });
      }

      return this.state.showAddressForm ? this.getAddressForm() : this.getSelector();
    }
  }]);

  return Addresses;
}(_react.default.Component);

var _default = Addresses;
exports.default = _default;