"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.visualizationModeCheck = exports.showReceiveOfferForm = exports.showItemIdentifier = exports.showCallToAction = exports.isSelectedAmountsUpdated = exports.isFallbackVisualizationMode = exports.isDesignerProductPreviewMode = exports.isCalculationAutoResolveEnabled = exports.hasMultipleVariantsSelected = exports.getVisualizationSettings = exports.getUserSelectedCalculation = exports.getShareUrl = exports.getSelectedVariantIdentifier = exports.getSelectedAmounts = exports.getItemSettings = exports.getItem = exports.getDeepestChildIdentifiers = exports.getCurrentVisualizationSettings = exports.getConfigurator = exports.getConfigurationSettings = exports.getConfiguration = exports.getClientTexts = exports.getClientHighlightColor = exports.getClient = exports.getChildren = exports.getCallToAction = exports.getCalculation = exports.getBulkNames = exports.findMaxChildrenCount = void 0;
var _get = _interopRequireDefault(require("lodash/get"));
var _flow = _interopRequireDefault(require("lodash/flow"));
var _find = _interopRequireDefault(require("lodash/find"));
var _difference = _interopRequireDefault(require("lodash/difference"));
var _keys = _interopRequireDefault(require("lodash/keys"));
var _values = _interopRequireDefault(require("lodash/values"));
var _createSelector = _interopRequireDefault(require("../../../Utils/Function/createSelector"));
var _Selectors = require("../ConfigurationMode/Selectors");
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 _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 _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 getConfigurator = exports.getConfigurator = function getConfigurator(rootState) {
  return rootState.configurator;
};
var getConfiguration = exports.getConfiguration = function getConfiguration(configuratorState) {
  return (0, _get["default"])(configuratorState, 'configuration');
};
var getCalculation = exports.getCalculation = function getCalculation(configuratorState) {
  return (0, _get["default"])(configuratorState, 'calculation');
};
var getSelectedVariantIdentifier = exports.getSelectedVariantIdentifier = function getSelectedVariantIdentifier(configuratorState) {
  return (0, _get["default"])(configuratorState, 'selectedVariantIdentifier');
};
var getClient = exports.getClient = function getClient(configuratorState) {
  return (0, _get["default"])(configuratorState, 'client');
};
var getItem = exports.getItem = (0, _createSelector["default"])(getConfiguration, function (configuration) {
  return (0, _get["default"])(configuration, 'item');
});
var getUserSelectedCalculation = exports.getUserSelectedCalculation = (0, _createSelector["default"])(getConfiguration, function (configuration) {
  return (0, _get["default"])(configuration, 'customdata.userSelectedCalculation');
});
var getSelectedAmounts = exports.getSelectedAmounts = (0, _createSelector["default"])(getConfiguration, function (configuration) {
  return (0, _get["default"])(configuration, 'customdata.selectedAmounts');
});
var getBulkNames = exports.getBulkNames = (0, _createSelector["default"])(getConfiguration, function (configuration) {
  return (0, _get["default"])(configuration, 'customdata.bulkNames') || [];
});
var getShareUrl = exports.getShareUrl = (0, _createSelector["default"])(getConfiguration, function (configuration) {
  return (0, _get["default"])(configuration, 'shareUrl', '') || '';
});
var getChildren = exports.getChildren = (0, _createSelector["default"])(getItem, function (item) {
  return (0, _get["default"])(item, 'itemGroup.children') || [];
});
var getItemSettings = exports.getItemSettings = (0, _createSelector["default"])(getItem, function (item) {
  return (0, _get["default"])(item, 'settings', {});
});
var getVisualizationSettings = exports.getVisualizationSettings = (0, _createSelector["default"])(getItemSettings, function (settings) {
  return {
    creator: (0, _get["default"])(settings, 'visualizationCreator'),
    designer: (0, _get["default"])(settings, 'visualizationDesigner')
  };
});
var findMaxChildrenCount = exports.findMaxChildrenCount = function findMaxChildrenCount(item) {
  var children = (0, _get["default"])(item, 'itemGroup.children') || [];
  return Math.max(children.length, children.length && findMaxChildrenCount(children[0]));
};
var getCurrentVisualizationSettings = exports.getCurrentVisualizationSettings = (0, _createSelector["default"])((0, _flow["default"])(getConfigurator, getVisualizationSettings), _Selectors.getSelectedConfigurationMode, function (visualizationSettings, configurationMode) {
  return visualizationSettings[configurationMode] || 'default';
});
var visualizationModeCheck = exports.visualizationModeCheck = (0, _flow["default"])(getCurrentVisualizationSettings, function (mode) {
  return {
    is2D: function is2D() {
      return mode === '2dVariant' || mode === '2dVariantOverlay' || mode === '2dLayer' || mode === '2d' || mode === 'layer';
    },
    is3D: function is3D() {
      return mode === '3dVariant' || mode === '3dScene' || mode === '3d';
    },
    is2DOverlay: function is2DOverlay() {
      return mode === '2dVariantOverlay';
    },
    isDefault: function isDefault() {
      return mode === 'default';
    }
  };
});
var isSelectedAmountsUpdated = exports.isSelectedAmountsUpdated = function isSelectedAmountsUpdated(configuratorState) {
  return (0, _get["default"])(configuratorState, 'selectedAmountsUpdated');
};
var hasMultipleVariantsSelected = exports.hasMultipleVariantsSelected = function hasMultipleVariantsSelected(state) {
  var item = state.configuration.item;
  var selectedAmounts = getSelectedAmounts(state);
  var selectedVariantKeys = (0, _keys["default"])(selectedAmounts);
  // get the firstSelectedAmountVariant and compare remaining to firstSelectedAmountVariant identifiers
  if (selectedVariantKeys.length > 1) {
    var firstSelectedVariant = (0, _find["default"])((0, _get["default"])(item, 'itemGroup.children'), {
      identifier: selectedVariantKeys[0]
    });
    if (!firstSelectedVariant) {
      return false;
    }
    if (!(0, _get["default"])(firstSelectedVariant, 'itemGroup.children')) {
      return true;
    }
    var multipleVariants = (0, _difference["default"])((0, _keys["default"])(selectedAmounts), (0, _values["default"])(getDeepestChildIdentifiers(firstSelectedVariant)));
    if (multipleVariants.length > 0) {
      return true;
    }
  }
  return false;
};
var getDeepestChildIdentifiers = exports.getDeepestChildIdentifiers = function getDeepestChildIdentifiers(item) {
  if (!item.itemGroup || !item.itemGroup.children) {
    return _defineProperty({}, item.itemgroupentryIdentifier, item.identifier);
  }
  var result = {};
  var _iterator = _createForOfIteratorHelper(item.itemGroup.children),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var child = _step.value;
      Object.assign(result, getDeepestChildIdentifiers(child));
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return result;
};
var getClientHighlightColor = exports.getClientHighlightColor = function getClientHighlightColor(state) {
  return (0, _get["default"])(getClient(getConfigurator(state)), 'theme.highlightColor');
};
var getClientTexts = exports.getClientTexts = function getClientTexts(configuratorState) {
  return (0, _get["default"])(getClient(configuratorState), 'texts') || {};
};
var isCalculationAutoResolveEnabled = exports.isCalculationAutoResolveEnabled = function isCalculationAutoResolveEnabled(_ref2) {
  var calculationAutoResolve = _ref2.calculationAutoResolve;
  return calculationAutoResolve === true;
};
var isDesignerProductPreviewMode = exports.isDesignerProductPreviewMode = function isDesignerProductPreviewMode() {
  var pathname = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.location.pathname;
  return /\/designer-preview-widget\/?/.test(pathname);
};
var getConfigurationSettings = exports.getConfigurationSettings = (0, _createSelector["default"])(getConfiguration, function (configuration) {
  return (0, _get["default"])(configuration, 'information.settings', {});
});
var showItemIdentifier = exports.showItemIdentifier = (0, _createSelector["default"])(getConfigurationSettings, function (settings) {
  return (0, _get["default"])(settings, 'show_item_identifier', false);
});
var getCallToAction = exports.getCallToAction = (0, _createSelector["default"])(getItem, function (item) {
  return (0, _get["default"])(item, 'callToAction', 'addToBasket');
});
var showCallToAction = exports.showCallToAction = (0, _createSelector["default"])(getCallToAction, function (callToAction) {
  return callToAction !== 'none';
});
var showReceiveOfferForm = exports.showReceiveOfferForm = (0, _createSelector["default"])(getCallToAction, function (callToAction) {
  return callToAction === 'requestOffer';
});
var isFallbackVisualizationMode = exports.isFallbackVisualizationMode = function isFallbackVisualizationMode(state) {
  return 'fallback' === getCurrentVisualizationSettings(state);
};