"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.RuleCounter = void 0;
var _react = _interopRequireDefault(require("react"));
var _isArray = _interopRequireDefault(require("lodash/isArray"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _DialogContent = _interopRequireDefault(require("@material-ui/core/DialogContent"));
var _Button = _interopRequireDefault(require("@material-ui/core/Button"));
var _styles = require("@material-ui/core/styles");
var _i18n = require("../../../../../Framework/i18n");
var _CustomDialog = _interopRequireDefault(require("../../../../../Shared/Components/CustomDialog"));
var _OptionRule = _interopRequireDefault(require("./OptionRule"));
var _RuleComponent = _interopRequireDefault(require("./RuleComponent"));
var _OptionExclusionRule = _interopRequireDefault(require("./OptionExclusionRule"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = _getPrototypeOf(t); if (r) { var s = _getPrototypeOf(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return _possibleConstructorReturn(this, e); }; }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var styles = function styles(theme) {
  return {
    incompatibilityDialog: {
      '& .MuiDialog-container': {
        '& .MuiDialog-paperWidthMd': {
          maxWidth: 420
        }
      }
    },
    incompatibilityRule: {
      '& .optionRule': _defineProperty({
        '& .text': {
          fontSize: 14,
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center'
        }
      }, theme.breakpoints.down('sm'), {
        '& .text': {
          height: '150px !important'
        }
      })
    },
    incompatibilityFooter: _defineProperty({
      padding: '10px 32px 32px 24px',
      textAlign: 'right',
      '& button': {
        marginLeft: 10,
        background: theme.palette.primary.darkText,
        color: 'white',
        '&:hover': {
          cursor: 'pointer',
          backgroundColor: '#616161'
        }
      }
    }, theme.breakpoints.down('xs'), {
      '& button': {
        marginLeft: 0,
        marginBottom: 10
      }
    })
  };
};
var RuleCounter = exports.RuleCounter = /*#__PURE__*/_createClass(function RuleCounter() {
  var _this = this;
  _classCallCheck(this, RuleCounter);
  _defineProperty(this, "values", {});
  _defineProperty(this, "postInc", function (key) {
    if (undefined === _this.values[key]) {
      _this.values[key] = 0;
    }
    var res = _this.values[key];
    _this.values[key]++;
    return res;
  });
  _defineProperty(this, "add", function (ruleResult) {
    return {
      checkedOptionIndex: ruleResult.checked_option ? _this.postInc(ruleResult.checked_option.identifier) : null,
      ruleTypeIndex: _this.postInc(ruleResult.rule_type)
    };
  });
});
var Incompatibility = /*#__PURE__*/function (_React$Component) {
  _inherits(Incompatibility, _React$Component);
  var _super = _createSuper(Incompatibility);
  function Incompatibility() {
    var _this2;
    _classCallCheck(this, Incompatibility);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this2 = _super.call.apply(_super, [this].concat(args));
    _defineProperty(_assertThisInitialized(_this2), "ruleTypeComponents", {
      optionexclusion: _OptionExclusionRule["default"],
      optiondependency: _OptionRule["default"],
      itemattributematch: _OptionRule["default"]
    });
    return _this2;
  }
  _createClass(Incompatibility, [{
    key: "renderRules",
    value: function renderRules() {
      var _this3 = this;
      var _this$props = this.props,
        checkResults = _this$props.checkResults,
        ruleComponentProps = _this$props.ruleComponentProps,
        optionclassifications = _this$props.optionclassifications,
        classes = _this$props.classes;
      var ruleCounter = new RuleCounter();
      return (0, _isArray["default"])(checkResults.check_results) && checkResults.check_results.map(function (result, key) {
        if (result.status === false) {
          var _ruleCounter$add = ruleCounter.add(result),
            checkedOptionIndex = _ruleCounter$add.checkedOptionIndex,
            ruleTypeIndex = _ruleCounter$add.ruleTypeIndex;
          var RuleTypeComponent = _this3.ruleTypeComponents[result.rule_type] || _RuleComponent["default"];
          return /*#__PURE__*/_react["default"].createElement(RuleTypeComponent, _objectSpread(_objectSpread({}, ruleComponentProps), {}, {
            key: key,
            result: result,
            checkedOptionIndex: checkedOptionIndex,
            ruleTypeIndex: ruleTypeIndex,
            optionclassifications: optionclassifications,
            classes: classes
          }));
        }
        return null;
      });
    }
  }, {
    key: "resolveConflicts",
    value: function resolveConflicts() {
      var resolveConflicts = this.props.resolveConflicts;
      resolveConflicts();
    }
  }, {
    key: "renderFooter",
    value: function renderFooter() {
      var classes = this.props.classes;
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: classes.incompatibilityFooter
      }, /*#__PURE__*/_react["default"].createElement(_Button["default"], {
        onClick: this.props.onClose,
        variant: "contained"
      }, (0, _i18n.T)('incompatibility.cancelButton')), /*#__PURE__*/_react["default"].createElement(_Button["default"], {
        onClick: this.resolveConflicts.bind(this),
        variant: "contained"
      }, (0, _i18n.T)('incompatibility.okButton')));
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
        checkResults = _this$props2.checkResults,
        show = _this$props2.show,
        onClose = _this$props2.onClose,
        classes = _this$props2.classes;
      if (!checkResults) {
        return null;
      }
      return /*#__PURE__*/_react["default"].createElement(_CustomDialog["default"], {
        open: show,
        dialogTitle: (0, _i18n.T)('incompatibility.dialogTitle'),
        onClose: onClose,
        onCancel: onClose,
        className: classes.incompatibilityDialog
      }, /*#__PURE__*/_react["default"].createElement(_DialogContent["default"], {
        className: 'incompatibility'
      }, this.renderRules()), this.renderFooter());
    }
  }]);
  return Incompatibility;
}(_react["default"].Component);
Incompatibility.defaultProps = {
  ruleComponentProps: {}
};
var _default = exports["default"] = (0, _styles.withStyles)(styles, {
  name: 'Incompatibility'
})(Incompatibility);