'use strict';

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

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

/**
 * This Utils Class processes Data Item from Query Result
 **/
define(['underscore'], function (_) {
	var Utils = function () {
		function Utils() {
			_classCallCheck(this, Utils);
		}

		/**
   * Normalize a dataItem to make sure it has a complete format of {d:xxx, u:xxx, p:{d:xxx, u:xxxx}} (if p is present)
   * @param {QueryResultDataItem} dataItem
   */
		Utils.normalizeData = function normalizeData(dataItem) {
			this._normalizeTuplePart(dataItem);
			if (dataItem && dataItem.p !== undefined) {
				this._normalizeTuplePart(dataItem.p);
			}
		};

		Utils.isRectifyNeeded = function isRectifyNeeded(tuplePart) {
			return tuplePart === null; //Augment this if necessary
		};

		/**
   * Only create a JS Object if the tuple part is returned not as an Object
   */


		Utils.getRectifiedData = function getRectifiedData(tuplePart) {
			return {
				'u': tuplePart
			};
		};

		/**
   * @param @param {Object} part is normalized only if it is an Object
   */


		Utils._normalizeTuplePart = function _normalizeTuplePart(part) {
			if (_.isObject(part)) {
				//TODO: once the queryResult Api is fully hooked up, we should clean here using dataCell.value only
				if (!part.hasOwnProperty('d')) {
					part.d = part.hasOwnProperty('u') ? part.u : part.hasOwnProperty('v') ? part.v : part.hasOwnProperty('value') ? part.value : part;
				}
				if (!part.hasOwnProperty('u')) {
					part.u = part.hasOwnProperty('d') ? part.d : part.hasOwnProperty('v') ? part.v : part.hasOwnProperty('value') ? part.value : part;
				}
			}
		};

		/**
   * TODO Need to review whether to expose this in QueryResultAPI or keep it here.
   * @return two dimensions array that stores the resolved values from query result data data points
   */


		Utils.getResolvedDataRows = function getResolvedDataRows(resultAPI) {
			var itemList = resultAPI.getResultItemList();
			var aResult = [];
			for (var i = 0; i < resultAPI.getRowCount(); i++) {
				aResult[i] = [];
				for (var j = 0; j < itemList.length; j++) {
					aResult[i].push(resultAPI.getValue(i, j));
				}
			}
			return aResult;
		};

		Utils.getDataItemIndex = function getDataItemIndex(queryResultData, dataItemRefID) {
			var nIndex = -1;
			if (queryResultData) {
				_.find(queryResultData.dataItems, function (dataItem, index) {
					var id = dataItem.itemClass && dataItem.itemClass['id'];
					if (id === dataItemRefID) {
						nIndex = index;
						return true;
					}
				});
			}
			return nIndex;
		};

		/**
   * Returns total number of data items
   *
   * @param resultItemList
   * @return Number - the dataitem count in all resultItems
   */


		Utils.getDataItemCount = function getDataItemCount(resultItemList) {
			return resultItemList.reduce(function (accumulator, currentValue) {
				return accumulator + currentValue.getDataItemList().length;
			}, 0);
		};
		/**
   * Returns array of data items
   *
   * @param resultItemList
   * @return array - the data items returned from query result
   */


		Utils.getResultDataItemList = function getResultDataItemList(resultItemList) {
			var dataItemList = [];
			resultItemList.forEach(function (item) {
				dataItemList.push.apply(dataItemList, item.getDataItemList());
			});
			return dataItemList;
		};

		return Utils;
	}();

	return Utils;
});
//# sourceMappingURL=QueryResultDataUtils.js.map