"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getDesignElementCount = exports.getDesignElementAvailability = exports.checkDesignElementRestrictionsWithRemovalCount = exports.checkDesignElementRestrictions = void 0;
var _isInteger = _interopRequireDefault(require("lodash/isInteger"));
var _get = _interopRequireDefault(require("lodash/get"));
var _Selectors = require("./Selectors");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
var getDesignElementCount = exports.getDesignElementCount = function getDesignElementCount(designData) {
  var objects = (0, _get["default"])(designData, 'canvasData.objects', []);
  var imagesCount = objects.filter(function (_ref) {
    var type = _ref.type;
    return type === 'Image';
  }).length;
  var textsCount = objects.filter(function (_ref2) {
    var type = _ref2.type;
    return type === 'Text';
  }).length;
  return {
    imagesCount: imagesCount,
    textsCount: textsCount,
    totalCount: imagesCount + textsCount
  };
};
var checkDesignElementRestrictions = exports.checkDesignElementRestrictions = function checkDesignElementRestrictions(designData, designProductionMethod, targetElementType) {
  var type = typeof targetElementType === 'string' && targetElementType.toLowerCase();
  var maxImages = designProductionMethod.maxImages,
    maxTexts = designProductionMethod.maxTexts,
    maxElements = designProductionMethod.maxElements;
  var _getDesignElementCoun = getDesignElementCount(designData),
    imagesCount = _getDesignElementCoun.imagesCount,
    textsCount = _getDesignElementCoun.textsCount,
    totalCount = _getDesignElementCoun.totalCount;
  if (maxElements > 0 && totalCount >= maxElements) {
    return {
      type: 'maxElements',
      max: maxElements
    };
  } else if ((!type || type === 'image') && maxImages > 0 && imagesCount >= maxImages) {
    return {
      type: 'maxImages',
      max: maxImages
    };
  } else if ((!type || type === 'text') && maxTexts > 0 && textsCount >= maxTexts) {
    return {
      type: 'maxTexts',
      max: maxTexts
    };
  }
  return null;
};
var checkDesignElementRestrictionsWithRemovalCount = exports.checkDesignElementRestrictionsWithRemovalCount = function checkDesignElementRestrictionsWithRemovalCount(designData, designProductionMethod) {
  var maxImages = designProductionMethod.maxImages,
    maxTexts = designProductionMethod.maxTexts,
    maxElements = designProductionMethod.maxElements;
  var _getDesignElementCoun2 = getDesignElementCount(designData),
    imagesCount = _getDesignElementCoun2.imagesCount,
    textsCount = _getDesignElementCoun2.textsCount,
    totalCount = _getDesignElementCoun2.totalCount;
  if ((0, _isInteger["default"])(maxElements) && totalCount > maxElements) {
    return {
      type: 'maxElements',
      max: maxElements,
      toBeRemovedCount: totalCount - maxElements
    };
  } else if ((0, _isInteger["default"])(maxImages) && imagesCount > maxImages) {
    return {
      type: 'maxImages',
      max: maxImages,
      toBeRemovedCount: imagesCount - maxImages
    };
  } else if ((0, _isInteger["default"])(maxTexts) && textsCount > maxTexts) {
    return {
      type: 'maxTexts',
      max: maxTexts,
      toBeRemovedCount: textsCount - maxTexts
    };
  }
  return null;
};
var getDesignElementAvailability = exports.getDesignElementAvailability = function getDesignElementAvailability(state) {
  var _getSelectedDesignPro = (0, _Selectors.getSelectedDesignProductionMethod)(state),
    maxImages = _getSelectedDesignPro.maxImages,
    maxTexts = _getSelectedDesignPro.maxTexts;
  return {
    image: !(0, _isInteger["default"])(maxImages) || maxImages > 0,
    text: !(0, _isInteger["default"])(maxTexts) || maxTexts > 0
  };
};