'use strict';

/*
 *+------------------------------------------------------------------------+
 *| Licensed Materials - Property of IBM
 *| IBM Cognos Products: BI Dashboard
 *| (C) Copyright IBM Corp. 2017
 *|
 *| US Government Users Restricted Rights - Use, duplication or disclosure
 *| restricted by GSA ADP Schedule Contract with IBM Corp.
 *+------------------------------------------------------------------------+
 */
define(['./VisQueryPostProcessor'], function (BaseClass) {
	'use strict';

	/**
 	 * This Class does Query result response post-processing such as processing the original query result
 	 * into a data structure that is required by GeoInfo, Data for small multiples visualization etc.
 	 **/

	var PostProcessorClass = BaseClass.extend({
		COL_MULTIPLIER_LIMIT: 30,
		ROW_MULTIPLIER_LIMIT: 30,
		_colMultiplier: null,
		_rowMultiplier: null,

		/*
  	 *@Constructor
  	 *@param {Object} options
  	 *				  for example: {'rowMultiplier': {'dataItemId': 'Sheet1.ProductLine','limit': 8}, 'rowMultiplier':{}}
  	 */
		init: function init(options) {
			PostProcessorClass.inherited('init', this, arguments);
			this._rowMultiplier = options.rowMultiplier;
			this._colMultiplier = options.colMultiplier;
		},

		getRowMultiplierLimit: function getRowMultiplierLimit() {
			if (this._rowMultiplier && this._rowMultiplier.limit !== null && this._rowMultiplier.limit !== undefined && this._rowMultiplier.limit > 0) {
				return this._rowMultiplier.limit;
			}
			return this.ROW_MULTIPLIER_LIMIT;
		},

		getRowMultiplierDataItem: function getRowMultiplierDataItem() {
			if (this._rowMultiplier) {
				return this._rowMultiplier.dataItemId;
			}
			return null;
		},

		/*Make COL multiplier limit getter a separated function*/
		getColMultiplierLimit: function getColMultiplierLimit() {
			if (this._colMultiplier && this._colMultiplier.limit !== null && this._colMultiplier.limit !== undefined && this._colMultiplier.limit > 0) {
				return this._colMultiplier.limit;
			}
			return this.COL_MULTIPLIER_LIMIT;
		},

		getColMultiplierDataItem: function getColMultiplierDataItem() {
			if (this._colMultiplier) {
				return this._colMultiplier.dataItemId;
			}
			return null;
		},

		/**
  	 *Process Query result based on the passed options to constructor.
  	 *For instance, limit the rows of datapoints so that in the response, for
  	 *specified dataitem, 'product', there will be only three tuples returned
  	 *
  	 *@return {QueryResultData}
  	 **/
		_processData: function _processData() {
			if (!this._queryResultData || !this._queryResultData.data) {
				return this._queryResultData;
			}
			var oInfo = this._getMultipliersInfo();
			var nRowMDataItemIndex = oInfo.nRowMDataItemIndex;
			var nColMDataItemIndex = oInfo.nColMDataItemIndex;
			var nRowMLimit = oInfo.nRowMLimit;
			var nColMLimit = oInfo.nColMLimit;
			//Clip DataItems
			if (nRowMLimit >= 0 && nRowMDataItemIndex >= 0 || nColMLimit >= 0 && nColMDataItemIndex) {
				this._clipDataItems(nRowMDataItemIndex, nRowMLimit);
				this._clipDataItems(nColMDataItemIndex, nColMLimit);
				//Clip DataPoints
				var nClipped = this._clipDatapoints(nRowMLimit, nRowMDataItemIndex, nColMLimit, nColMDataItemIndex);
				//Set ProcessInfo
				var oProcessInfo = {
					'clipped': nClipped
				};
				if (nRowMLimit >= 0) {
					oProcessInfo.rowLimit = nRowMLimit;
				}
				if (nColMLimit >= 0) {
					oProcessInfo.colLimit = nColMLimit;
				}
				this._setProcessInfo(oProcessInfo);
			}
			//Process Done
			return this._queryResultData;
		},

		_getMultipliersInfo: function _getMultipliersInfo() {
			var nRowMLimit = this.getRowMultiplierLimit();
			var nColMLimit = this.getColMultiplierLimit();
			var nRowMDataItemIndex, nColMDataItemIndex; //The data item index that the data item maps to in the query result response

			var sRowMDataItemId = this.getRowMultiplierDataItem();
			if (sRowMDataItemId) {
				nRowMDataItemIndex = this._queryResultData.data.getDataItemIndex(sRowMDataItemId);
			}

			var sColMDataItemId = this.getColMultiplierDataItem();
			if (sColMDataItemId) {
				nColMDataItemIndex = this._queryResultData.data.getDataItemIndex(sColMDataItemId);
			}

			return {
				'nRowMLimit': nRowMLimit,
				'nRowMDataItemIndex': nRowMDataItemIndex,
				'nColMLimit': nColMLimit,
				'nColMDataItemIndex': nColMDataItemIndex
			};
		},

		_clipDatapoints: function _clipDatapoints(nRowMLimit, nRowMDataItemIndex, nColMLimit, nColMDataItemIndex) {
			var oData = this._queryResultData.data._resultData.data;
			var nOrigLength = oData.length;
			var nCounter = 0;
			while (nOrigLength > 0) {
				if (nRowMDataItemIndex >= 0 && oData[nOrigLength - 1].pt[nRowMDataItemIndex] > nRowMLimit - 1 || nColMDataItemIndex >= 0 && oData[nOrigLength - 1].pt[nColMDataItemIndex] > nColMLimit - 1) {
					oData.splice(nOrigLength - 1, 1); //Avoid create new Array
					nCounter++;
				}
				nOrigLength--;
			}
			return nCounter;
		},

		_clipDataItems: function _clipDataItems(dataItemIndex, limit) {
			if (this._queryResultData && limit >= 0 && dataItemIndex >= 0) {
				var aDataItems = this._queryResultData.data.getResultDataItems();
				if (aDataItems[dataItemIndex] && aDataItems[dataItemIndex].getTupleCount() > 0) {
					aDataItems[dataItemIndex].clipTuples(limit);
				}
			}
		}
	});

	return PostProcessorClass;
});
//# sourceMappingURL=PostProcessSmallMultiple.js.map