"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _ComponentContainer = _interopRequireDefault(require("../../../../Framework/ComponentContainer"));
var _InvalidConfigurationNotice = _interopRequireDefault(require("../../Components/InvalidConfigurationNotice"));
var _Actions = require("../../../Reducers/UI/Actions");
var _Selectors = require("../../../Reducers/Configurator/Selectors");
var _cached = require("../../../../Utils/Function/cached");
var _configuration = require("../../../configuration");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
/**
 * Stores the mapped validation errors in cache to keep the same reference if the source has not changed
 * this makes possible to show up the dialog when it gets new list of errors.
 * @param configuratorState
 * @return {*}
 */
var getValidationErrors = function getValidationErrors(configuratorState) {
  var validation = configuratorState.validation;
  var _getConfiguration = (0, _Selectors.getConfiguration)(configuratorState),
    optionclassifications = _getConfiguration.optionclassifications;
  return (0, _cached.getCached)('getValidationErrors', validation, optionclassifications && optionclassifications.length).use(function () {
    if (validation.code && validation.code === 'minimum_order_amount_error') {
      var _errors = [];
      var itemTitle = '';
      validation.errors.forEach(function (error, index) {
        itemTitle = error.itemTitle;
        _errors.push({
          message: 'minimumOrderAmountError',
          key: index,
          amounts: {
            minAmount: error.minimumAmount,
            selectedAmount: error.selectedAmount
          }
        });
      });
      return [{
        title: itemTitle,
        errors: _errors
      }];
    }
    var errors = (validation === null || validation === void 0 ? void 0 : validation.validationErrors) || (validation === null || validation === void 0 ? void 0 : validation.errors) || {};
    return !optionclassifications ? [] : Object.entries(errors).map(function (_ref) {
      var _optionclassification;
      var _ref2 = _slicedToArray(_ref, 2),
        key = _ref2[0],
        error = _ref2[1];
      return {
        title: ((_optionclassification = optionclassifications.find(function (_ref3) {
          var identifier = _ref3.identifier;
          return identifier === key;
        })) === null || _optionclassification === void 0 ? void 0 : _optionclassification.title) || error.componentTitle,
        errors: error.errors,
        key: key
      };
    });
  });
};
var mapStateToProps = function mapStateToProps(_ref4) {
  var configurator = _ref4.configurator,
    ui = _ref4.ui;
  var validationErrors = getValidationErrors(configurator);
  return {
    adminMode: (0, _configuration.getConf)('adminMode'),
    Component: _InvalidConfigurationNotice["default"],
    validationErrors: validationErrors,
    show: !!ui.visibilityMap.validationResult
  };
};
var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return {
    close: function close() {
      return dispatch((0, _Actions.setVisible)('validationResult', false));
    },
    open: function open() {
      return dispatch((0, _Actions.setVisible)('validationResult', true));
    }
  };
};
var _default = exports["default"] = (0, _ComponentContainer["default"])(mapStateToProps, mapDispatchToProps);