'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

/**
 * Licensed Materials - Property of IBM
 * IBM Business Analytics (C) Copyright IBM Corp. 2020
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

/**
 * @class LiveWidgetSegmentImpl
 * @hideconstructor
 * @classdesc
 */

define(['../../../lib/@waca/dashboard-common/dist/core/APIFactory', './api/LiveWidgetSegmentAPI'], function (APIFactory, LiveWidgetSegmentAPI) {
	var LiveWidgetSegment = function () {
		function LiveWidgetSegment(options) {
			_classCallCheck(this, LiveWidgetSegment);

			this.dashboard = options.features['API'];
			this.visDefinitions = options.features['VisDefinitions'];
			this.logger = options.features['Logger'];

			this._api = APIFactory.createAPI(this, [LiveWidgetSegmentAPI]);
		}

		LiveWidgetSegment.prototype.getAPI = function getAPI() {
			if (!this.api) {
				this.api = APIFactory.createAPI(this, [LiveWidgetSegmentAPI]);
			}
			return this.api;
		};

		LiveWidgetSegment.prototype.track = function track() {
			var event = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

			var supportedTrackActions = ['fromRecommendation', 'Recommended', 'Other', 'colorPalette'];
			if (supportedTrackActions.indexOf(event.category) === -1) {
				return;
			}

			var segment = this.dashboard.getFeature('segment');
			if (!segment) {
				this.logger.error('Invalid segment object');
				return;
			}

			var widgetType = event.visualization ? event.visualization.getDefinition().getLabel() : null;

			var payload = void 0,
			    action = void 0;

			if (!widgetType && event.category === 'fromRecommendation') {
				// Dragged a slot to the canvas
				widgetType = event.recommendation;
				action = 'created';
				payload = {
					type: 'Created Object',
					widgetSource: 'fromRecommendation',
					details: {
						widgetSource: 'fromRecommendation',
						'widgetType': event.recommendation
					}
				};
			} else if (event.category === 'fromRecommendation' || event.category === 'Recommended' || event.category === 'Other') {
				// Modified the type
				action = 'modified';
				// The visChangerFlyoutView passes the type or the id depending the type or id for JQGrid or Crosstabs
				var visDef = this.visDefinitions.getByType(event.info.visId) || this.visDefinitions.getById(event.info.visId) || null;
				var newWidgetType = visDef && visDef.getLabel();
				payload = {
					type: 'Updated Object',
					details: {
						'propertyId': event.category,
						'property.newValue': newWidgetType,
						'property.oldValue': widgetType
					}
				};
			} else if (event.category === 'colorPalette') {
				// Modified the colorPalette property
				action = 'modified';
				payload = {
					type: 'Updated Object',
					details: {
						'propertyId': event.category,
						'property.newValue': event.info.newValue,
						'property.oldValue': event.info.previousValue
					}
				};
			}

			if (payload) {
				var result = _extends({}, payload, {
					details: _extends({}, payload.details, {
						'widgetType': widgetType,
						'assetType': this.dashboard.getApplicationName()
					}),
					trackType: 'data_widget',
					'custom.widgetType': widgetType
				});

				if (event.content) {
					result.object = event.content.getId();
				}
				var assetId = this.dashboard.getDashboardInfo().boardId;
				if (assetId) {
					result.details.assetId = assetId;
				}
				segment.track(action, function () {
					return result;
				});
			}
		};

		return LiveWidgetSegment;
	}();

	return LiveWidgetSegment;
});
//# sourceMappingURL=LiveWidgetSegment.js.map