"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.vectorFormats = exports.showVectorizeImageQuality = exports.isImageReadyToBeAdded = exports.isImageCompatible = exports.isHardModified = exports.isEditableVectorImage = exports.isEditMode = exports.isColorPreviewCompatible = exports.isColorMappingCompatible = exports.isAddMode = exports.getUserImagesByDesignArea = exports.getUserImage = exports.getPreviewUrlFromImageData = exports.getImageUsageInfo = void 0;
var _get = _interopRequireDefault(require("lodash/get"));
var _find = _interopRequireDefault(require("lodash/find"));
var _isString = _interopRequireDefault(require("lodash/isString"));
var _flatten = _interopRequireDefault(require("lodash/flatten"));
var _configuration = require("../../configuration");
var _getUrl = require("../../Utils/getUrl");
var _Selectors = require("../Configurator/Selectors");
var _Reducer = require("../DesignData/Reducer");
var _Actions = require("./Actions");
var _Selectors2 = require("../DesignArea/Selectors");
var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var vectorFormats = exports.vectorFormats = ['ai', 'pdf', 'svg', 'eps', 'ps', 'ept'];
var getPreviewUrlFromImageData = exports.getPreviewUrlFromImageData = function getPreviewUrlFromImageData(preview) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  var previewUrl = (0, _isString["default"])(preview) ? preview : preview.url;
  if (/^data:image/.test(previewUrl)) {
    return previewUrl;
  }
  return (0, _getUrl.getUrl)(previewUrl, options.host);
};
var isEditableVectorImage = exports.isEditableVectorImage = function isEditableVectorImage(preview) {
  return preview && preview.url && (/.svg$/.test(preview.url) || /^data:image\/svg\+xml/.test(preview.url));
};
var getUserImage = exports.getUserImage = function getUserImage(state, identifier) {
  var userImages = (0, _get["default"])(state, 'imageGallery.userImages', []);
  return (0, _find["default"])(userImages, {
    identifier: identifier
  });
};
var getUserImagesByDesignArea = exports.getUserImagesByDesignArea = function getUserImagesByDesignArea(configuratorState) {
  var configuration = (0, _Selectors.getConfiguration)(configuratorState);
  var designData = (0, _get["default"])(configuration, 'designdata');
  if (!designData) {
    return null;
  }
  return (0, _flatten["default"])(Object.values(designData) // merge the objects into one array
  .filter(function (dd) {
    return !!dd.canvasData;
  }) // get the areas with existing canvas data
  .map(function (data) {
    return data.canvasData.objects;
  })) // get the objects array from the areas
  .filter(function (o) {
    return o.type === 'Image';
  }) // filter the images
  .map(function (object) {
    return object.imageData;
  }) // get the imageData objects from the images
  .filter(function (imageData, idx, arr) {
    return !imageData.isPlaceHolderImage &&
    // placeholders are not needed in the list
    // uniq by identifier
    imageData === arr.find(function (_ref) {
      var identifier = _ref.identifier;
      return identifier === imageData.identifier;
    });
  });
};
var getImageUsageInfo = exports.getImageUsageInfo = function getImageUsageInfo(state, imageIdentifier) {
  if (!imageIdentifier) {
    imageIdentifier = (0, _get["default"])(state.imageGallery.selectedImage, 'identifier');
  }
  if (!imageIdentifier) {
    return {
      count: 0,
      designAreas: 0
    };
  }
  return Object.values((0, _Reducer.getDesignData)((0, _Selectors.getConfigurator)(state))).reduce(function (_ref2, _ref3) {
    var count = _ref2.count,
      designAreas = _ref2.designAreas;
    var canvasData = _ref3.canvasData;
    var countPerArea = canvasData && canvasData.objects ? canvasData.objects.filter(function (_ref4) {
      var imageData = _ref4.imageData;
      return imageData && imageData.identifier === imageIdentifier;
    }).length : 0;
    return {
      designAreas: designAreas + (countPerArea ? 1 : 0),
      count: count + countPerArea
    };
  }, {
    count: 0,
    designAreas: 0
  });
};
var isEditMode = exports.isEditMode = function isEditMode(state) {
  return (0, _get["default"])(state, 'imageGallery.mode') === _Actions.ImageGalleryModes.edit;
};
var isAddMode = exports.isAddMode = function isAddMode(state) {
  return (0, _get["default"])(state, 'imageGallery.mode') === _Actions.ImageGalleryModes.add;
};
var isImageCompatible = exports.isImageCompatible = function isImageCompatible(state, imageData) {
  return (0, _Selectors2.areHashesCompatible)(imageData.compatibilityInfoHash, (0, _Selectors2.getCurrentCompatibilityInfoHash)(state));
};
var isImageReadyToBeAdded = exports.isImageReadyToBeAdded = function isImageReadyToBeAdded(state, imageData) {
  return Boolean(imageData && isAddMode(state) && isImageCompatible(state, imageData) && getImageUsageInfo(state, imageData.identifier).count > 0 // make sure it's already added (add it one more time)
  );
};

var isColorMappingCompatible = exports.isColorMappingCompatible = function isColorMappingCompatible(colorPalettes, vectorizeColorsMap) {
  if (!colorPalettes) {
    return false;
  }
  var colors = Object.values(vectorizeColorsMap);
  return !colors.length || !colors.map(function (_ref5) {
    var identifier = _ref5.identifier;
    return JSON.parse(identifier).palette;
  }).some(function (identifier) {
    return !(0, _find["default"])(colorPalettes, {
      identifier: identifier
    });
  });
};
var showVectorizeImageQuality = exports.showVectorizeImageQuality = function showVectorizeImageQuality(imageToAnalyse) {
  var minimumPixelAmount = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  if (imageToAnalyse) {
    var pixelsInTheImage = (0, _get["default"])(imageToAnalyse, 'original.width', 0) * (0, _get["default"])(imageToAnalyse, 'original.height', 0);
    var hasVectorFormat = vectorFormats.indexOf((0, _get["default"])(imageToAnalyse, 'original.format', '').toLowerCase()) > -1;
    var pixelsOfReference = minimumPixelAmount || (0, _configuration.getConf)('vectorizeImageQuality');
    return !hasVectorFormat && pixelsInTheImage < pixelsOfReference;
  }
  return false;
};
var isHardModified = exports.isHardModified = function isHardModified(originalImageData, newImageData) {
  return ['operations', 'compatibilityInfoHash'].some(function (prop) {
    return !(0, _isEqual["default"])(originalImageData[prop], newImageData[prop]);
  });
};
var isColorPreviewCompatible = exports.isColorPreviewCompatible = function isColorPreviewCompatible(state, image, designProductionMethodIdentifier) {
  if (!image) {
    image = state.imageGallery.selectedImage;
  }
  var designProductionMethod = designProductionMethodIdentifier ? (0, _Selectors2.getDesignProductionMethodByIdentifier)(state, designProductionMethodIdentifier) : (0, _Selectors2.getSelectedDesignProductionMethod)(state);
  var colorPreviewRequired = (0, _Selectors2.designProductionMethodHasEmbroideryVisualizationEffect)(designProductionMethod) || (0, _Selectors2.isVectorizedLogoMandatory)(designProductionMethod);
  var displayColorPreview = image && (!!image.gallery || !!image.displayColorPreview);
  // do not let the user to add a raster image if the production method is embroidery
  return !colorPreviewRequired || displayColorPreview || false;
};