'use strict';

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

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

/*
 *+------------------------------------------------------------------------+
 *| Licensed Materials - Property of IBM
 *| IBM Cognos Products: BI Dashboard
 *| (C) Copyright IBM Corp. 2018
 *|
 *| US Government Users Restricted Rights - Use, duplication or disclosure
 *| restricted by GSA ADP Schedule Contract with IBM Corp.
 *+------------------------------------------------------------------------+
 */

define(['underscore', '../../../lib/@waca/upgrades/UpgradeBase', '../../../lib/@waca/dashboard-common/dist/core/UniqueHashIdBuilder'], function (_, UpgradeBase, UniqueHashIdBuilder) {

	/**
  * Upgrade data item ids in dashboard spec for better performance to avoid querying same data items with different ids.
  **/
	var DataItemIdUpgrade = function (_UpgradeBase) {
		_inherits(DataItemIdUpgrade, _UpgradeBase);

		function DataItemIdUpgrade() {
			_classCallCheck(this, DataItemIdUpgrade);

			var _this = _possibleConstructorReturn(this, _UpgradeBase.call(this));

			_this.VERSION = 1014;
			return _this;
		}

		/**
   * Perform upgrade
   *
   * @param {object} spec - spec to perform upgrade on
   *
   * @return {Promise} Promise to be resolved when upgrade performed
   */


		DataItemIdUpgrade.prototype.up = function up(spec) {
			var _this2 = this;

			if (!spec || !spec.widgets || _.isEmpty(spec.widgets)) {
				return Promise.resolve(spec);
			}

			for (var key in spec.widgets) {
				var widget = spec.widgets[key];
				var dataViews = widget.data && widget.data.dataViews;
				if (dataViews) {
					(function () {
						var dataItemIdsMap = {};
						dataViews.forEach(function (dataView) {
							dataView.dataItems.forEach(function (dataItem) {
								if (dataItem.id !== '_multiMeasuresSeries') {
									dataItemIdsMap[dataItem.id] = UniqueHashIdBuilder.createUniqueHashId(dataItem.itemId, dataItemIdsMap);
								}
							});
						});
						spec.widgets[key] = _this2._upgradeDataItemIds(widget, dataItemIdsMap);
					})();
				}
			}

			return Promise.resolve(spec);
		};

		// Downgrades are not available in CA


		DataItemIdUpgrade.prototype.down = function down(spec) {
			return Promise.resolve(spec);
		};

		/**
   * Replace all the old dataItem id with the new unique hash id in the widget
   * @param {object} widget 
   * @param {map} dataItemIdsMap - key is dataItem.id and value is unique hash id 
   */


		DataItemIdUpgrade.prototype._upgradeDataItemIds = function _upgradeDataItemIds(widget, dataItemIdsMap) {
			var widgetStr = JSON.stringify(widget);
			for (var key in dataItemIdsMap) {
				var re = new RegExp('"' + key + '"', 'g');
				widgetStr = widgetStr.replace(re, '"' + dataItemIdsMap[key] + '"');
			}
			return JSON.parse(widgetStr);
		};

		return DataItemIdUpgrade;
	}(UpgradeBase);

	return new DataItemIdUpgrade();
});
//# sourceMappingURL=DataItemIdUpgrade.js.map