"use strict";

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

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

var _modularityFront = _interopRequireDefault(require("modularity-front"));

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

require("./mini-cart.css");

var _ShoppingCart = _interopRequireDefault(require("@material-ui/icons/ShoppingCart"));

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

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 _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return 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 _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); }

function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }

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 Module = _modularityFront.default.Module;
var Link = _modularityFront.default.Modules.Link;

var MiniCartModule =
/*#__PURE__*/
function (_Modularity$Module) {
  _inherits(MiniCartModule, _Modularity$Module);

  function MiniCartModule(props) {
    var _this;

    _classCallCheck(this, MiniCartModule);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(MiniCartModule).call(this, props));
    _this.state = {
      products: {}
    };

    _this.addProduct.onUpdate(function (value) {
      if (!value || !value.id) {
        return;
      }

      var products = _objectSpread({}, _this.state.products);

      var key = value.id;
      var val = {
        image: value.image,
        name: value.name,
        price: value.price,
        quantity: value.quantity
      };
      console.log(val); // Thats me punk writing this code below !!!

      key in products ? products[key].quantity += val.quantity : products[key] = val;

      _this.setState({
        products: products
      });

      _this._save(products);

      _this.products.set(products);
    });

    _this._load();

    return _this;
  }

  _createClass(MiniCartModule, [{
    key: "_minBut",
    value: function _minBut(e, id) {
      var products = this.state.products;

      if (products[id].quantity > 1) {
        products[id].quantity -= 1;
      } else {
        delete products[id];
      }

      this.setState({
        products: products
      });

      this._save(products);

      this.products.set(products);
    }
  }, {
    key: "_addBut",
    value: function _addBut(e, id) {
      var products = this.state.products;
      products[id].quantity += 1;
      this.setState({
        products: products
      });
      this.products.set(products);

      this._save(products);

      return false;
    }
  }, {
    key: "_save",
    value: function _save(products) {
      localStorage.setItem("cart_state", JSON.stringify(products));
    }
  }, {
    key: "_load",
    value: function _load() {
      var local = localStorage.getItem('cart_state');

      if (local) {
        this.state.products = JSON.parse(local);
        this.products.set(this.state.products);
      }
    }
  }, {
    key: "cartPage",
    value: function cartPage() {
      console.log("this");
      this.CoreSystem.go("/orders");
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var prods = this.state.products;
      var nameList = Object.keys(prods).map(function (id, index) {
        return _react.default.createElement("li", {
          key: id,
          className: "custom-flex basket-row"
        }, _react.default.createElement("div", null, _react.default.createElement("img", {
          className: "circle mini_img",
          src: "/yuna.jpg"
        })), _react.default.createElement("div", {
          className: "flex-prod-name"
        }, _react.default.createElement(Link, {
          link: "/product/" + id
        }, prods[id].name)), _react.default.createElement("div", {
          className: "cust-flex"
        }, _react.default.createElement("div", null, _react.default.createElement("span", {
          onClick: function onClick(e) {
            _this2._minBut(e, id);
          },
          className: "quantity_but"
        }, "\u2212")), _react.default.createElement("div", null, prods[id].quantity), _react.default.createElement("div", null, _react.default.createElement("span", {
          onClick: function onClick(e) {
            _this2._addBut(e, id);
          },
          className: "quantity_but"
        }, "+"))), _react.default.createElement("div", {
          className: "prod_price"
        }, prods[id].price, " \u20AC"));
      });
      var selector = false;

      if (Object.keys(prods).length > 0) {
        selector = true;
      }

      var total = Object.keys(prods).reduce(function (total, key) {
        return total + prods[key].quantity * prods[key].price;
      }, 0).toFixed(2);
      console.log("############", total);
      return _react.default.createElement("div", {
        className: "dropdown " + (selector ? "HAHAHA" : "NO"),
        style: {
          verticalAlign: 'bottom',
          display: 'flex',
          height: 64,
          justifyContent: 'center',
          flexDirection: 'column'
        }
      }, _react.default.createElement(_ShoppingCart.default, {
        onClick: function onClick(e) {
          return _this2.cartPage();
        },
        className: "cart_size"
      }), selector ? _react.default.createElement("div", {
        className: "dropdown-cont cart",
        key: Math.random()
      }, _react.default.createElement("div", {
        className: "cart_title"
      }, _DictionaryService.default.get("Your Cart")), _react.default.createElement("div", {
        className: "cart_products"
      }, _react.default.createElement("ol", null, nameList)), _react.default.createElement(_reactMaterialize.Row, {
        className: "cart_total"
      }, _react.default.createElement("span", null, _DictionaryService.default.get("Total")), _react.default.createElement("b", null, total, " \u20AC")), _react.default.createElement(_reactMaterialize.Row, null, _react.default.createElement("div", {
        className: "btn order_but",
        onClick: function onClick(e) {
          return _this2.cartPage();
        }
      }, _DictionaryService.default.get("Checkout")))) : _react.default.createElement("div", {
        className: "dropdown-cont cart empty",
        key: Math.random()
      }, _react.default.createElement("div", {
        className: "empty-cart"
      }, _DictionaryService.default.get("Empty cart"))), _get(_getPrototypeOf(MiniCartModule.prototype), "render", this).call(this));
    }
  }]);

  return MiniCartModule;
}(_modularityFront.default.Module);

MiniCartModule.EditPosition = "center";
MiniCartModule.Inputs = {
  cartImage: {
    type: Module.Types.Text,
    defaultValue: "/mini_cart.png"
  },
  addProduct: {
    type: Module.Types.Object
  }
};
MiniCartModule.Outputs = {
  products: {}
};
MiniCartModule.Category = "sxoinas";
var _default = MiniCartModule;
exports.default = _default;