"use strict";

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

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

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

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

var _Components = require("./Components");

var ErrorModal = function ErrorModal(_ref) {
  var onClose = _ref.onClose,
      open = _ref.open,
      products = _ref.products,
      setProducts = _ref.setProducts;

  var _React$useState = _react["default"].useState('Products'),
      page = _React$useState[0],
      setPage = _React$useState[1];

  var _React$useState2 = _react["default"].useState(),
      variant = _React$useState2[0],
      setVariant = _React$useState2[1]; // rerender modal when products change


  var _React$useReducer = _react["default"].useReducer(function (x) {
    return x + 1;
  }, 0),
      _update = _React$useReducer[1];

  var contentMap = {
    Products: /*#__PURE__*/_react["default"].createElement(_Components.Products, {
      products: products,
      setRestock: function setRestock() {
        return setPage('Restock');
      },
      setVariant: setVariant,
      setProducts: setProducts,
      variant: variant,
      update: function update() {
        return _update();
      }
    }),
    Restock: /*#__PURE__*/_react["default"].createElement(_Components.Restock, {
      backToProducts: function backToProducts() {
        return setPage('Products');
      },
      variant: variant,
      setProducts: setProducts,
      products: products
    })
  };
  var titleMap = {
    Products: 'Items out of stock',
    Restock: 'Notify me when the product is available'
  };

  _react["default"].useEffect(function () {
    if (products && products.length < 1) {
      onClose();
    }
  }, [products, products == null ? void 0 : products.length, onClose]);

  return /*#__PURE__*/_react["default"].createElement(_Common.Modal, {
    open: open,
    handleClose: onClose,
    title: titleMap[page]
  }, /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, contentMap[page]));
};

exports.ErrorModal = ErrorModal;
//# sourceMappingURL=index.js.map