"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.updateLinkedImagesWithImageData = exports.updateImageData = exports.showSwitchToProductionMethodWithoutVectorization = exports.showSwitchToProductionMethodWithVectorization = exports.showProductionMethodRemoveImagesConfirm = exports.showProductionMethodChangeConfirm = exports.showProductionMethodChangeColorsConfirm = exports.showMinFontSizeConfirm = exports.showMaxColorChangeConfirm = exports.showColorUsageConfirm = exports.setInitialDesignData = exports.setDesignProductionMethod = exports.setDesignAreaTextsColor = exports.setColorAmount = exports.setCanvasData = exports.replaceDesignAreaTextsColor = exports.createDesignDataAction = exports.actionTypes = void 0;
var _i18n = require("../../../Framework/i18n");
var _Actions = require("../Confirm/Actions");
var _Actions2 = require("../DesignArea/Actions");
var _Selectors = require("../DesignArea/Selectors");
var _Reducer = require("./Reducer");
var _Selectors2 = require("../Configurator/Selectors");
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 actionTypes = exports.actionTypes = {
  SET_CANVAS_DATA: 'DesignData/SET_CANVAS_DATA',
  SET_DESIGN_PRODUCTION_METHOD: 'DesignData/SET_DESIGN_PRODUCTION_METHOD',
  SET_COLOR_AMOUNT: 'DesignData/SET_COLOR_AMOUNT',
  SET_INITIAL_DESIGN_DATA: 'DesignData/SET_INITIAL_DESIGN_DATA',
  UPDATE_IMAGE_DATA: 'DesignData/UPDATE_IMAGE_DATA',
  SET_DESIGN_AREA_TEXTS_COLOR: 'DesignData/SET_DESIGN_AREA_TEXTS_COLOR',
  REPLACE_DESIGN_AREA_TEXTS_COLOR: 'DesignData/REPLACE_DESIGN_AREA_TEXTS_COLOR',
  SET_CANVAS_DATA_MAP: 'DesignData/SET_CANVAS_DATA_MAP'
};
var createDesignDataAction = exports.createDesignDataAction = function createDesignDataAction(actionObject) {
  return function (dispatch, getState) {
    var _getState = getState(),
      designAreaIdentifier = _getState.designArea.selectedDesignArea.identifier;
    // skip if no designAreaIdentifier
    if (designAreaIdentifier) {
      return dispatch(_objectSpread(_objectSpread({}, actionObject), {}, {
        designAreaIdentifier: designAreaIdentifier
      }));
    }
  };
};
var setCanvasData = exports.setCanvasData = function setCanvasData(canvasData, targetDesignAreaIdentifier) {
  return function (dispatch, getState) {
    var _getState2 = getState(),
      selectedDesignArea = _getState2.designArea.selectedDesignArea;
    var designAreaIdentifier = targetDesignAreaIdentifier || selectedDesignArea.identifier;
    // skip if no designAreaIdentifier
    if (designAreaIdentifier) {
      dispatch((0, _Actions2.setPreviousEditedDesignAreaIdentier)(designAreaIdentifier));
      return dispatch({
        type: actionTypes.SET_CANVAS_DATA,
        canvasData: canvasData,
        designAreaIdentifier: designAreaIdentifier
      });
    }
  };
};
var setDesignProductionMethod = exports.setDesignProductionMethod = function setDesignProductionMethod(designProductionMethodIdentifier) {
  return createDesignDataAction({
    type: actionTypes.SET_DESIGN_PRODUCTION_METHOD,
    designProductionMethodIdentifier: designProductionMethodIdentifier
  });
};
var updateImageData = exports.updateImageData = function updateImageData(imageData) {
  var originalImageIdentifier = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
  return function (dispatch, getState) {
    var designAreaIdentifier = (0, _Selectors.getSelectedDesignArea)(getState()).identifier;
    return dispatch({
      type: actionTypes.UPDATE_IMAGE_DATA,
      imageData: imageData,
      designAreaIdentifier: designAreaIdentifier,
      originalImageIdentifier: originalImageIdentifier
    });
  };
};
var setColorAmount = exports.setColorAmount = function setColorAmount(colorAmount) {
  return createDesignDataAction({
    type: actionTypes.SET_COLOR_AMOUNT,
    colorAmount: colorAmount
  });
};
var setInitialDesignData = exports.setInitialDesignData = function setInitialDesignData(initialDesignData) {
  return {
    type: actionTypes.SET_INITIAL_DESIGN_DATA,
    initialDesignData: initialDesignData
  };
};
var showSwitchToProductionMethodWithVectorization = exports.showSwitchToProductionMethodWithVectorization = function showSwitchToProductionMethodWithVectorization() {
  return (0, _Actions.showConfirm)((0, _i18n.t)('designProductionMethodSwitch.toVectorization.message'), (0, _i18n.t)('designProductionMethodSwitch.toVectorization.ok'), (0, _i18n.t)('designProductionMethodSwitch.toVectorization.cancel'));
};
var showSwitchToProductionMethodWithoutVectorization = exports.showSwitchToProductionMethodWithoutVectorization = function showSwitchToProductionMethodWithoutVectorization() {
  return (0, _Actions.showConfirm)((0, _i18n.t)('designProductionMethodSwitch.fromVectorization.message'), (0, _i18n.t)('designProductionMethodSwitch.fromVectorization.ok'), (0, _i18n.t)('designProductionMethodSwitch.fromVectorization.cancel'));
};
var showColorUsageConfirm = exports.showColorUsageConfirm = function showColorUsageConfirm(colorCount, maxColorCount) {
  return (0, _Actions.showConfirm)((0, _i18n.t)('colorAmountAlert.colorChange', {
    colorAmount: colorCount,
    maxColorAmount: maxColorCount
  }), (0, _i18n.t)('colorAmountAlert.colorChangeOk', {
    colorAmount: colorCount
  }), (0, _i18n.t)('colorAmountAlert.colorChangeCancel', {
    colorAmount: maxColorCount
  }));
};
var showMaxColorChangeConfirm = exports.showMaxColorChangeConfirm = function showMaxColorChangeConfirm(colorCount, maxColorCount, currentColorCount) {
  return (0, _Actions.showConfirm)((0, _i18n.t)('colorAmountAlert.maxColorChange', {
    colorAmount: colorCount,
    maxColorAmount: maxColorCount
  }), (0, _i18n.t)('colorAmountAlert.maxColorChangeOk', {
    colorAmount: maxColorCount
  }), (0, _i18n.t)('colorAmountAlert.maxColorChangeCancel', {
    colorAmount: currentColorCount
  }));
};
var showMinFontSizeConfirm = exports.showMinFontSizeConfirm = function showMinFontSizeConfirm(fontSizeMin) {
  return (0, _Actions.showConfirm)((0, _i18n.t)('minFontSizeWarning.message', {
    fontSizeMin: fontSizeMin
  }), (0, _i18n.t)('minFontSizeWarning.okButtonLabel'), (0, _i18n.t)('minFontSizeWarning.cancelButtonLabel'));
};
var showProductionMethodRemoveImagesConfirm = exports.showProductionMethodRemoveImagesConfirm = function showProductionMethodRemoveImagesConfirm() {
  return (0, _Actions.showConfirm)((0, _i18n.t)('colorAmountAlert.productionMethodRemoveImagesWarning.message'), (0, _i18n.t)('colorAmountAlert.productionMethodRemoveImagesWarning.okButtonLabel'), (0, _i18n.t)('colorAmountAlert.productionMethodRemoveImagesWarning.cancelButtonLabel'));
};
var showProductionMethodChangeColorsConfirm = exports.showProductionMethodChangeColorsConfirm = function showProductionMethodChangeColorsConfirm() {
  return (0, _Actions.showConfirm)((0, _i18n.t)('colorAmountAlert.productionMethodChangeColorsWarning.message'), (0, _i18n.t)('colorAmountAlert.productionMethodChangeColorsWarning.okButtonLabel'), (0, _i18n.t)('colorAmountAlert.productionMethodChangeColorsWarning.cancelButtonLabel'));
};
var showProductionMethodChangeConfirm = exports.showProductionMethodChangeConfirm = function showProductionMethodChangeConfirm() {
  return (0, _Actions.showConfirm)((0, _i18n.t)('designProductionChangeAlert.message'), (0, _i18n.t)('designProductionChangeAlert.okButtonLabel'), (0, _i18n.t)('designProductionChangeAlert.cancelButtonLabel'));
};
var setDesignAreaTextsColor = exports.setDesignAreaTextsColor = function setDesignAreaTextsColor(identifier, color) {
  return {
    type: actionTypes.SET_DESIGN_AREA_TEXTS_COLOR,
    identifier: identifier,
    color: color
  };
};
var replaceDesignAreaTextsColor = exports.replaceDesignAreaTextsColor = function replaceDesignAreaTextsColor(identifier, oldColorData, oldValue, colorData, value) {
  return {
    type: actionTypes.REPLACE_DESIGN_AREA_TEXTS_COLOR,
    identifier: identifier,
    oldColorData: oldColorData,
    oldValue: oldValue,
    colorData: colorData,
    value: value
  };
};
var updateLinkedImagesWithImageData = exports.updateLinkedImagesWithImageData = function updateLinkedImagesWithImageData(imageIdentifier, imageData) {
  return function (dispatch, getState) {
    var designData = (0, _Reducer.getDesignData)((0, _Selectors2.getConfigurator)(getState()));
    var canvasDataMap = {};
    Object.keys(designData).filter(function (designAreaIdentifier) {
      var canvasData = designData[designAreaIdentifier].canvasData;
      return canvasData && canvasData.objects && canvasData.objects.length > 0;
    }).forEach(function (designAreaIdentifier) {
      var canvasData = designData[designAreaIdentifier].canvasData;
      var objects = canvasData.objects;
      canvasDataMap[designAreaIdentifier] = _objectSpread(_objectSpread({}, canvasData), {}, {
        objects: objects && objects.map(function (object) {
          if (object.type === 'Image' && object.imageData.identifier === imageIdentifier) {
            return _objectSpread(_objectSpread({}, object), {}, {
              imageData: imageData
            });
          }
          return object;
        })
      });
    });
    dispatch({
      type: actionTypes.SET_CANVAS_DATA_MAP,
      canvasDataMap: canvasDataMap
    });
  };
};