"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getClosestColorFromMap = exports.expandVectorizeColorsMap = void 0;
var _findIndex = _interopRequireDefault(require("lodash/findIndex"));
var _DesignDataService = require("../../../Services/DesignDataService");
var _Selectors = require("../../../Reducers/DesignArea/Selectors");
var _Selectors2 = require("../../../Reducers/DesignData/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 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 removeColorFromIndexArray = function removeColorFromIndexArray(colorIndexArray, colorDataStr) {
  var _parseColorData = (0, _Selectors.parseColorData)(colorDataStr),
    palette = _parseColorData.palette,
    identifier = _parseColorData.color;
  var idx = (0, _findIndex["default"])(colorIndexArray, {
    palette: palette,
    identifier: identifier
  });
  idx !== -1 && colorIndexArray.splice(idx, 1);
};

/**
 * @param {object} colorsMap
 * @param {array} colorPalettes
 * @param {number} requiredColors
 */
var expandVectorizeColorsMap = exports.expandVectorizeColorsMap = function expandVectorizeColorsMap(colorsMap, colorPalettes, requiredColors) {
  var availableColors = (0, _DesignDataService.getColorPalettesIndex)(colorPalettes);
  var expandedColorsMap = _objectSpread({}, colorsMap);
  var aggregatedColors = (0, _Selectors2.getAggregatedColors)(colorsMap);
  var usedColors = Object.values(aggregatedColors);
  usedColors.forEach(function (_ref) {
    var identifier = _ref.identifier;
    return removeColorFromIndexArray(availableColors, identifier);
  });
  var colorsToFind = requiredColors - usedColors.length;
  if (colorsToFind < 1) {
    return expandedColorsMap;
  }
  for (var _i = 0, _Object$keys = Object.keys(colorsMap); _i < _Object$keys.length; _i++) {
    var colorIdx = _Object$keys[_i];
    // skip if a color is already mapped to the key
    if (aggregatedColors[colorIdx]) {
      continue;
    }
    var replacementColor = (0, _DesignDataService.getClosestColorPaletteColor)((0, _DesignDataService.getRgbValuesFromString)((0, _Selectors.hexToRgb)(colorIdx)), availableColors);
    if (!replacementColor) {
      break;
    }
    expandedColorsMap[colorIdx] = _objectSpread({}, replacementColor);
    removeColorFromIndexArray(availableColors, replacementColor.identifier);
    if (! --colorsToFind || !availableColors.length) {
      break;
    }
  }
  return expandedColorsMap;
};
var getClosestColorFromMap = exports.getClosestColorFromMap = function getClosestColorFromMap(colorsMap, hexColor) {
  var availableColors = Object.values(colorsMap).filter(function (_ref2) {
    var value = _ref2.value;
    return value !== hexColor;
  }).map(function (color) {
    var _parseColorData2 = (0, _Selectors.parseColorData)(color.identifier),
      palette = _parseColorData2.palette,
      identifier = _parseColorData2.color;
    return {
      palette: palette,
      identifier: identifier,
      valueObject: (0, _Selectors.hexToRgb)(color.value, false)
    };
  });
  return (0, _DesignDataService.getClosestColorPaletteColor)((0, _Selectors.hexToRgb)(hexColor, false), availableColors);
};