"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getThumbnailData = exports.getSelectedDesignViewIdentifier = exports.getDesignViewLength = exports.getCurrentDesignView = exports.get2DModelSrc = void 0;
var _get = _interopRequireDefault(require("lodash/get"));
var _Selectors = require("../Configurator/Selectors");
var _Selectors2 = require("../DesignArea/Selectors");
var _Transformers = require("./Transformers");
var _Transformers2 = require("../DesignArea/Transformers");
var _Selectors3 = require("../ConfigurationMode/Selectors");
var _getImageUrl = _interopRequireDefault(require("../../Utils/getImageUrl"));
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 _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
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 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 _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 getThumbnailData = exports.getThumbnailData = function getThumbnailData(state) {
  var items = state.designView.designViews.map(function (designView) {
    var thumbnailImage = state.designView.viewThumbnailImages[designView.identifier];
    return _objectSpread({
      identifier: designView.identifier
    }, thumbnailImage && {
      thumbnailImage: (0, _getImageUrl["default"])(thumbnailImage)
    });
  });
  var dotMode = !items.every(function (item) {
    return item.thumbnailImage;
  });
  var currentConfiguration = (0, _Selectors3.getSelectedConfigurationMode)(state);

  // on designer, we fall back to the older thumbnail images, if they exist
  if (currentConfiguration === 'designer' && dotMode === true) {
    var configuratorState = (0, _Selectors.getConfigurator)(state);
    var itemIdentifier = (0, _Selectors.getSelectedVariantIdentifier)(configuratorState) || (0, _Selectors.getItem)(configuratorState).identifier;
    var designFallBackItems = state.designView.designViews.map(function (designView) {
      var thumbnailImage = (0, _get["default"])(designView.thumbnails, itemIdentifier);
      return _objectSpread({
        identifier: designView.identifier
      }, thumbnailImage && {
        thumbnailImage: (0, _getImageUrl["default"])(thumbnailImage)
      });
    });
    return {
      items: designFallBackItems,
      dotMode: !designFallBackItems.every(function (item) {
        return item.thumbnailImage;
      })
    };
  }
  return {
    dotMode: dotMode,
    items: items
  };
};
var get2DModelSrc = exports.get2DModelSrc = function get2DModelSrc(state, host) {
  var configuratorState = (0, _Selectors.getConfigurator)(state);
  var itemIdentifier = (0, _Selectors.getSelectedVariantIdentifier)(configuratorState) || (0, _Selectors.getItem)(configuratorState).identifier;
  var designArea = (0, _Selectors2.getSelectedDesignArea)(state);
  var designView = designArea && designArea.designView || state.designView.selectedDesignView;
  var src = (0, _get["default"])(designView, "images.".concat(itemIdentifier), null);
  return src ? "".concat(host).concat(src) : null;
};
var getDesignViewLength = exports.getDesignViewLength = function getDesignViewLength(state) {
  return (0, _get["default"])(state.designView.designViews, 'length', 0);
};
var getSelectedDesignViewIdentifier = exports.getSelectedDesignViewIdentifier = function getSelectedDesignViewIdentifier(state) {
  return state.designView.selectedDesignView.identifier;
};

/**
 * Returns the selected design view with the current design area data, or false if not selected
 * @param {{designView: {}, designArea: {}}} state
 * @return {*|{designAreas: *}|boolean}
 */
var getCurrentDesignView = exports.getCurrentDesignView = function getCurrentDesignView(state) {
  var selectedDesignView = state.designView.selectedDesignView;
  if (!selectedDesignView || !selectedDesignView.identifier) {
    return false;
  }
  if (!selectedDesignView.designAreas) {
    return selectedDesignView;
  }
  return _objectSpread(_objectSpread({}, selectedDesignView), {}, {
    designAreas: _toConsumableArray(selectedDesignView.designAreas.map(function (designArea) {
      return (0, _Transformers2.recalculateSingleDesignArea)(state, designArea);
    })).sort(_Transformers.compareDesignAreasBySize)
  });
};