"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 = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _CustomDialog = _interopRequireDefault(require("../../../../../Shared/Components/CustomDialog"));
var _CustomDialogSelect = _interopRequireDefault(require("../../../../../Shared/Components/CustomDialogSelect"));
var _MainButton = _interopRequireDefault(require("../../../../../Shared/Components/MainButton"));
var _FastConfigurationRow = _interopRequireDefault(require("../FastConfigurationRow"));
var _FastConfigurationSelect = _interopRequireDefault(require("../FastConfigurationSelect"));
var _i18n = require("../../../../../Framework/i18n");
require("./Styles.scss");
var _isArray = _interopRequireDefault(require("lodash/isArray"));
var _merge = _interopRequireDefault(require("lodash/merge"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
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 _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; }
var FastConfiguration = function FastConfiguration(_ref) {
  var optionclassifications = _ref.optionclassifications,
    options = _ref.options,
    FastConfigurationRow = _ref.FastConfigurationRow,
    FastConfigurationSelect = _ref.FastConfigurationSelect,
    onOptionclassificationSelect = _ref.onOptionclassificationSelect,
    onOptionSelect = _ref.onOptionSelect,
    isLoading = _ref.isLoading,
    languageSettings = _ref.languageSettings,
    channelSettings = _ref.channelSettings,
    PriceComponent = _ref.PriceComponent,
    calculation = _ref.calculation,
    onDoneClick = _ref.onDoneClick,
    show = _ref.show,
    onClose = _ref.onClose;
  var _useState = (0, _react.useState)({
      index: 0,
      content: {}
    }),
    _useState2 = _slicedToArray(_useState, 2),
    productPartSelected = _useState2[0],
    setProductPartSelected = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    _useState4 = _slicedToArray(_useState3, 2),
    isOptionsDialogOpen = _useState4[0],
    setOptionsDialogOpen = _useState4[1];
  var getStyles = function getStyles(index) {
    return {
      detailsDialog: {
        top: 'calc(103px + 50 * (' + index + 'px + 1px)',
        left: '40%',
        width: 10,
        height: 200,
        position: 'absolute'
      }
    };
  };
  var getSelectedOption = function getSelectedOption(optionClassification) {
    return optionClassification.selectedoptions ? optionClassification.selectedoptions.length === 1 ? optionClassification.selectedoptions[0] : optionClassification : undefined;
  };
  var getUpdatedOptions = function getUpdatedOptions() {
    var optionClassification = productPartSelected;
    var updatedOptions = [];
    var _iterator = _createForOfIteratorHelper(options),
      _step;
    try {
      for (_iterator.s(); !(_step = _iterator.n()).done;) {
        var option = _step.value;
        var uo = option;
        if ((0, _isArray["default"])(optionClassification.selectedoptions)) {
          var _iterator2 = _createForOfIteratorHelper(optionClassification.selectedoptions),
            _step2;
          try {
            for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
              var selectedOption = _step2.value;
              if (selectedOption.identifier === option.identifier) {
                uo = (0, _merge["default"])({}, option, selectedOption);
              } else if (option.option_group && (0, _isArray["default"])(option.option_group.children)) {
                var _iterator3 = _createForOfIteratorHelper(option.option_group.children),
                  _step3;
                try {
                  for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
                    var item = _step3.value;
                    if (selectedOption.identifier === item.identifier) {
                      uo = (0, _merge["default"])({}, option, selectedOption);
                      break;
                    }
                  }
                } catch (err) {
                  _iterator3.e(err);
                } finally {
                  _iterator3.f();
                }
              }
            }
          } catch (err) {
            _iterator2.e(err);
          } finally {
            _iterator2.f();
          }
        }
        updatedOptions.push(uo);
      }
    } catch (err) {
      _iterator.e(err);
    } finally {
      _iterator.f();
    }
    return updatedOptions;
  };
  var onFocus = function onFocus(optionClassification) {
    onOptionclassificationSelect(optionClassification, false);
  };
  var selectOptionAndCloseDialog = function selectOptionAndCloseDialog(option) {
    onOptionSelect(option);
    closeOptionsDialog();
  };
  var closeOptionsDialog = function closeOptionsDialog() {
    setOptionsDialogOpen(false);
  };
  var openOptionsDialog = function openOptionsDialog(index, content) {
    setOptionsDialogOpen(true);
    setProductPartSelected({
      index: index,
      content: content
    });
  };
  var toggleOptionsDialog = function toggleOptionsDialog(index) {
    var productPart = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    isOptionsDialogOpen ? closeOptionsDialog() : openOptionsDialog(index, productPart);
  };
  var renderRows = function renderRows() {
    return optionclassifications.map(function (optionClassification, index) {
      return /*#__PURE__*/_react["default"].createElement(FastConfigurationRow, {
        key: 'optionclassificationitem' + optionClassification.identifier,
        optionclassification: optionClassification
      }, /*#__PURE__*/_react["default"].createElement(FastConfigurationSelect, {
        optionClassification: optionClassification,
        options: options,
        onOptionclassificationSelect: onOptionclassificationSelect,
        onOptionSelect: onOptionSelect,
        isLoading: isLoading,
        languageSettings: languageSettings,
        channelSettings: channelSettings,
        closeDialog: closeOptionsDialog,
        getValue: getSelectedOption,
        onFocus: onFocus,
        rowIndex: index,
        toggleDialog: toggleOptionsDialog
      }));
    });
  };
  var renderHeader = function renderHeader() {
    return null;
  };
  var renderFooter = function renderFooter() {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "fastconfiguration-footer"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "fastconfiguration-price"
    }, /*#__PURE__*/_react["default"].createElement(PriceComponent, {
      calculation: calculation,
      title: (0, _i18n.T)('price.label')
    })), /*#__PURE__*/_react["default"].createElement(_MainButton["default"], {
      className: 'fastconfiguration-done',
      onClick: onDoneClick,
      variant: "contained"
    }, (0, _i18n.T)('fastconfiguration.toConfigurator')));
  };
  var renderComponent = function renderComponent() {
    var _getStyles = getStyles(productPartSelected.index),
      detailsDialog = _getStyles.detailsDialog;
    var selectedOption = getSelectedOption(productPartSelected.content);
    var updatedOptions = getUpdatedOptions();
    return /*#__PURE__*/_react["default"].createElement(_CustomDialog["default"], {
      open: show,
      onCancel: onClose,
      onClose: onClose,
      dialogTitle: (0, _i18n.T)('fastConfiguration.dialogTitle'),
      className: 'fastconfiguration-dialog'
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "fastconfiguration"
    }, renderHeader(), /*#__PURE__*/_react["default"].createElement("div", {
      className: "fastconfiguration-rows-wrapper"
    }, renderRows()), renderFooter()), isOptionsDialogOpen ? /*#__PURE__*/_react["default"].createElement("div", {
      style: detailsDialog
    }, /*#__PURE__*/_react["default"].createElement(_CustomDialogSelect["default"], {
      options: updatedOptions,
      show: isOptionsDialogOpen,
      isLoading: isLoading,
      onSelectOption: selectOptionAndCloseDialog,
      selectedOption: selectedOption,
      selectedOptionProperty: 'identifier'
    })) : null);
  };
  return renderComponent();
};
FastConfiguration.defaultProps = {
  options: [],
  optionclassifications: [],
  onOptionclassificationSelect: function onOptionclassificationSelect() {},
  onOptionSelect: function onOptionSelect() {},
  FastConfigurationRow: _FastConfigurationRow["default"],
  FastConfigurationSelect: _FastConfigurationSelect["default"],
  calculation: {},
  onDoneClick: function onDoneClick() {}
};
var _default = exports["default"] = FastConfiguration;