"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.actionsToTrack = void 0;
var _ServiceLocator = require("../ServiceLocator");
var _Constant = require("../Constant");
var _i18n = require("../../Framework/i18n");
var _Selectors = require("../Reducers/ConfigurationMode/Selectors");
var _filter = _interopRequireDefault(require("lodash/filter"));
var _map = _interopRequireDefault(require("lodash/map"));
var _actionsToTrack;
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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), 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 actionsToTrack = exports.actionsToTrack = (_actionsToTrack = {}, _defineProperty(_actionsToTrack, _Constant.EventTypes.load, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.save, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.save, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.mail, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.load_request, function (action) {
  return {
    event: action.type,
    configuration_code: action.data
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.change_variant, function (action) {
  return {
    event: action.type,
    selected_variant_identifier: action.data.key
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.preview_open, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.preview_close, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.add_text, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.add_image, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.uploaded_file_type, function (action) {
  return {
    event: action.type,
    file_type: action.data.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.edit_image, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.image_colorize, function (action) {
  return {
    event: action.type,
    selected_image_edit_colorize: action.data
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.image_edit_colorize_color, function (action) {
  return {
    event: action.type,
    selected_color: action.data
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.image_edit_colorize_found_colors, function (action) {
  return {
    event: action.type,
    color_amount: action.data
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.image_background, function (action) {
  return {
    event: action.type,
    image_edit_operation: (0, _i18n.t)('fileUpload.' + ({
      replace: 'removeBackgroundEverywhere',
      flood: 'removeBackgroundFromBorder'
    }[action.data] || 'notRemoveBackground'))
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.image_edit_options, function (action) {
  return {
    event: action.data === 0 ? 'image_edit_change_file' : action.data === 1 ? 'image_edit_remove_background' : 'image_edit_colorize'
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.upload_new, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.image_thumbnail_select, function (action) {
  return {
    event: action.type,
    selected_image: action.data
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.text_bold, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.text_font, function (action) {
  return {
    event: action.type,
    selected_font_name: action.data.key
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.text_italic, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.text_align_left, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.text_align_center, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.text_align_right, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.text_align_justify, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.text_color, function (action) {
  return {
    event: action.type,
    selected_color: action.data
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.align_horizontal_center, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.align_vertical_center, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.align_move_down, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.align_move_up, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.add_to_cart, function (action, state) {
  var selectedMode = state.configurationMode.selectedMode;
  var event = (0, _Selectors.isDesignerMode)(selectedMode) ? _Constant.EventTypes.add_to_cart : _Constant.EventTypes.configuration_add_to_cart;
  return {
    event: event,
    selected_amounts: state.configurator.configuration.customdata.selectedAmounts
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.selected_amounts, function (action, state) {
  var selectedMode = state.configurationMode.selectedMode;
  var event = (0, _Selectors.isDesignerMode)(selectedMode) ? _Constant.EventTypes.selected_amounts : _Constant.EventTypes.configuration_selected_amounts;
  return {
    event: event,
    selected_amounts: state.configurator.configuration.customdata.selectedAmounts,
    selected_design_areas: getSelectedDesignAreas(state),
    selected_design_production_methods: getSelectedDesignProductionMethods(state),
    selected_options: getSelectedOptions(state)
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.design_area_switch, function (action, state) {
  return {
    event: action.type,
    selected_design_area_identifier: state.designArea.selectedDesignArea.identifier
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.design_production_switch, function (action) {
  return {
    event: action.type,
    selected_design_production_method_identifier: action.data.key
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.widget_print_view, function (action) {
  return {
    event: action.type
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.widget_open_configurator, function (action, state) {
  return {
    event: action.type,
    selected_design_areas: getSelectedDesignAreas(state),
    selected_design_production_methods: getSelectedDesignProductionMethods(state),
    selected_options: getSelectedWidgetConfigurationOptions(state),
    selected_amount: state.configurator.configuration.customdata.selectedAmounts
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.widget_change_amount, function (action, state) {
  return {
    event: action.type,
    selected_amounts: state.configurator.configuration.customdata.selectedAmounts
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.widget_add_design_area, function (action) {
  return {
    event: action.type,
    selected_design_area_identifier: action.data
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.widget_remove_design_area, function (action) {
  return {
    event: action.type,
    removed_design_area_identifier: action.data
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.widget_change_design_production_method, function (action) {
  return {
    event: action.type,
    selected_design_production_method_identifier: action.data.key
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.load_item, function (action) {
  return {
    event: action.type,
    item_identifier: action.data
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.switch_option, function (action) {
  return {
    event: action.type,
    selected_component: action.data.component,
    selected_option: action.data.option
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.select_component, function (action) {
  return {
    event: action.type,
    selected_component: action.data
  };
}), _defineProperty(_actionsToTrack, _Constant.EventTypes.configuration_mode_switch, function (action) {
  return {
    event: action.type,
    switch_from: action.data === 'designer' ? 'creator' : 'designer',
    switch_to: action.data
  };
}), _actionsToTrack);
var getSelectedWidgetConfigurationOptions = function getSelectedWidgetConfigurationOptions(state) {
  var confoptions = [];
  var calculationObj = state.configurator.calculation.positions;
  for (var confoption in calculationObj) {
    if (calculationObj[confoption].selectableByUser && calculationObj[confoption].selected) {
      confoptions.push(calculationObj[confoption].title);
    }
  }
  return confoptions;
};
var getSelectedDesignAreas = function getSelectedDesignAreas(state) {
  var designAreas = [];
  var designData = state.configurator.configuration.designdata;
  for (var design in designData) {
    var canvasData = designData[design].canvasData;
    if (canvasData && canvasData.objects.length > 0) {
      designAreas.push(design);
    }
  }
  return designAreas;
};
var getSelectedDesignProductionMethods = function getSelectedDesignProductionMethods(state) {
  var designProductionMethods = [];
  var designData = state.configurator.configuration.designdata;
  for (var designProduction in designData) {
    var canvasData = designData[designProduction].canvasData;
    if (canvasData && canvasData.objects.length > 0) {
      designProductionMethods.push(designData[designProduction].designProductionMethodIdentifier);
    }
  }
  return designProductionMethods;
};
var getSelectedOptions = function getSelectedOptions(state) {
  var configuration = state.configurator.configuration;
  var optionclassifications = configuration.optionclassifications;
  var options = [];
  var componentsWithSelectedOptions = (0, _filter["default"])(optionclassifications, function (oc) {
    return oc.selectedoptions && oc.selectedoptions.length > 0;
  });
  componentsWithSelectedOptions.forEach(function (component) {
    var selectedComponentMap = {};
    selectedComponentMap[component.identifier] = (0, _map["default"])(component.selectedoptions, function (option) {
      return {
        identifier: option.identifier,
        amount: option.amount
      };
    });
    options.push(selectedComponentMap);
  });
  return options;
};
var AnalyticsService = exports["default"] = /*#__PURE__*/function () {
  function AnalyticsService() {
    _classCallCheck(this, AnalyticsService);
    _defineProperty(this, "EventTypes", _Constant.EventTypes);
  }
  _createClass(AnalyticsService, [{
    key: "setDataLayer",
    value: function setDataLayer(dataLayer) {
      this.dataLayer = dataLayer;
      var loadIdentifier = _ServiceLocator.Services.configurator.state.loadIdentifier;
      loadIdentifier && this.triggerEvent(_Constant.EventTypes.load_item, _ServiceLocator.Services.configurator.state.loadIdentifier);
    }
  }, {
    key: "getActionsToTrack",
    value: function getActionsToTrack() {
      return actionsToTrack;
    }
  }, {
    key: "triggerEvent",
    value: function triggerEvent(type) {
      var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var actionsToTrack = this.getActionsToTrack();
      if (type && actionsToTrack[type]) {
        var gaData = actionsToTrack[type]({
          type: type,
          data: data
        }, _ServiceLocator.Services.store.state);
        this.dataLayer && this.dataLayer.push(gaData);
      }
    }
  }]);
  return AnalyticsService;
}();
_defineProperty(AnalyticsService, "serviceName", 'AnalyticsService');