"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _get = _interopRequireDefault(require("lodash/get"));
var _has = _interopRequireDefault(require("lodash/has"));
var _isArray = _interopRequireDefault(require("lodash/isArray"));
var _Typography = _interopRequireDefault(require("@material-ui/core/Typography"));
var _OptionGroup = _interopRequireDefault(require("../OptionGroup"));
var _i18n = require("../../../../../Framework/i18n");
require("./Styles.scss");
var _SelectableRow = _interopRequireDefault(require("../FastConfiguration/SelectableRow"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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 _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 _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
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 _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); }
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); }
var FastConfigurationSelect = /*#__PURE__*/function (_React$Component) {
  _inherits(FastConfigurationSelect, _React$Component);
  var _super = _createSuper(FastConfigurationSelect);
  function FastConfigurationSelect(props) {
    var _this;
    _classCallCheck(this, FastConfigurationSelect);
    _this = _super.call(this, props);
    _this.optionElements = {};
    return _this;
  }
  _createClass(FastConfigurationSelect, [{
    key: "isOptionSelected",
    value: function isOptionSelected(option) {
      var optionclassification = this.props.optionclassification;
      var selected = false;
      if ((0, _isArray["default"])(optionclassification.selectedoptions)) {
        var _iterator = _createForOfIteratorHelper(optionclassification.selectedoptions),
          _step;
        try {
          for (_iterator.s(); !(_step = _iterator.n()).done;) {
            var selectedOption = _step.value;
            if (selectedOption.identifier === option.identifier) {
              selected = true;
            } else if (option.option_group && (0, _isArray["default"])(option.option_group.children)) {
              var _iterator2 = _createForOfIteratorHelper(option.option_group.children),
                _step2;
              try {
                for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
                  var item = _step2.value;
                  if (selectedOption.identifier === item.identifier) {
                    selected = true;
                    break;
                  }
                }
              } catch (err) {
                _iterator2.e(err);
              } finally {
                _iterator2.f();
              }
              if (selected) {
                break;
              }
            }
          }
        } catch (err) {
          _iterator.e(err);
        } finally {
          _iterator.f();
        }
      }
      return selected;
    }
  }, {
    key: "onItemGroupOptionSelect",
    value: function onItemGroupOptionSelect(option) {
      if (this.refs.dialogSelect) {
        this.refs.dialogSelect.closeDialog();
      }
      var onOptionSelect = this.props.onOptionSelect;
      onOptionSelect(_objectSpread(_objectSpread({}, option), {}, {
        amount: 1
      }));
    }
  }, {
    key: "isOptionEnabled",
    value: function isOptionEnabled(option) {
      return (0, _get["default"])(option, 'stock.traffic_light', false) !== 'red';
    }
  }, {
    key: "renderOptionGroupSelect",
    value: function renderOptionGroupSelect(option) {
      var _this2 = this;
      if (!this.isOptionEnabled(option)) {
        return null;
      }
      var optionClassification = this.props.optionClassification;
      if ((0, _has["default"])(option, 'option_group.children') && (0, _isArray["default"])(option.option_group.children) && option.option_group.children.length > 0) {
        return /*#__PURE__*/_react["default"].createElement(_Typography["default"], {
          className: "fastconfiguration-option-group-select",
          onClick: function onClick(e) {
            return e.stopPropagation();
          }
        }, /*#__PURE__*/_react["default"].createElement(_OptionGroup["default"], {
          itemGroup: option.option_group,
          option: option,
          optionclassification: optionClassification,
          onOptionSelect: this.onItemGroupOptionSelect.bind(this),
          relativeTo: function relativeTo() {
            if (!_this2.refs.dialogSelect) {
              return false;
            }
            if (_this2.refs.dialogSelect.state.isOpen) {
              return _this2.optionElements[option.identifier];
            } else {
              return _this2.refs.dialogSelect.control;
            }
          },
          dialogPositioning: [['left', 'left'], ['top', 'bottom']],
          adjustWidth: true,
          isSelected: this.isOptionSelected.bind(this),
          renderOption: this.renderOption.bind(this)
        }));
      }
      return null;
    }
  }, {
    key: "renderOption",
    value: function renderOption(option, isValue) {
      return /*#__PURE__*/_react["default"].createElement("div", {
        className: "fastconfiguration-option"
      }, /*#__PURE__*/_react["default"].createElement("span", {
        className: "fastconfiguration-option-title"
      }, option.title), !isValue && this.renderOptionGroupSelect(option), /*#__PURE__*/_react["default"].createElement("span", {
        className: "fastconfiguration-option-price"
      }, option.priceFormatted));
    }
  }, {
    key: "renderOwnOption",
    value: function renderOwnOption(option, isValue) {
      var _this3 = this;
      return /*#__PURE__*/_react["default"].cloneElement(this.renderOption(option, isValue), {
        ref: function ref(el) {
          return _this3.optionElements[option.identifier] = el;
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
        closeDialog = _this$props.closeDialog,
        getValue = _this$props.getValue,
        _onFocus = _this$props.onFocus,
        _toggleDialog = _this$props.toggleDialog,
        optionClassification = _this$props.optionClassification,
        rowIndex = _this$props.rowIndex;
      var value = getValue(optionClassification);
      return /*#__PURE__*/_react["default"].createElement(_SelectableRow["default"], {
        label: (0, _i18n.T)('fastConfiguration.select.placeholder'),
        selectedOption: value,
        showSelected: true,
        closeDialog: closeDialog,
        renderValue: this.renderValue,
        toggleDialog: function toggleDialog() {
          return _toggleDialog(rowIndex, optionClassification);
        },
        onFocus: function onFocus() {
          return _onFocus(optionClassification);
        },
        onOptionRendered: this.renderOwnOption.bind(this)
      });
    }
  }]);
  return FastConfigurationSelect;
}(_react["default"].Component);
FastConfigurationSelect.defaultProps = {
  OptionlistitemSelect: _OptionGroup["default"]
};
var _default = exports["default"] = FastConfigurationSelect;