'use strict';

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

/**
 * This Class encapsulates the raw query result data rows,its related information and
 * functions
 **/
define(['../../lib/@waca/core-client/js/core-client/ui/core/Class', 'underscore', '../../lib/@waca/dashboard-common/dist/query/FacetDataObject'], function (BaseClass, _, FacetClass) {

	var QueryResultData = BaseClass.extend({
		_dataRows: [],

		init: function init(data) {
			QueryResultData.inherited('init', this, arguments);
			this._dataRows = data;
		},

		/**
   * @return a JSON object that has attributes of 'display' and 'use'
   * for the given raw array element's index
   **/
		getDataItemByIndex: function getDataItemByIndex(nRow, nCol) {
			return QueryResultData.toDataItem(this._dataRows[!nRow ? 0 : nRow][!nCol ? 0 : nCol]);
		},

		/**
   * @return an array of JSON objects that has attributes of 'display' and 'use'
   * for the given a row index
   **/
		getDataItemsByRowIndex: function getDataItemsByRowIndex(nRow) {
			var dataItems = [];
			_.each(this._dataRows[!nRow ? 0 : nRow], function (data) {
				dataItems.push(QueryResultData.toDataItem(data));
			}.bind(this));
			return dataItems;
		},

		getRowsSize: function getRowsSize() {
			return this._dataRows ? this._dataRows.length : 0;
		},

		getColsSizeByRowIndex: function getColsSizeByRowIndex(index) {
			var aRow = this._dataRows[index];
			return aRow ? aRow.length : 0;
		},

		//TODO: Keep this function for RAVE2 for now
		getDataRows: function getDataRows() {
			return this._dataRows;
		}
	});

	/**
  *@return a JSON object that has attributes of 'display' and 'use'
  * for a given raw array's row object and its column's index
  */
	QueryResultData.getDataItemByColumnIndex = function (oRow, nCol) {
		if (oRow && nCol >= 0) {
			return QueryResultData.toDataItem(oRow[nCol]);
		}
		return null;
	};

	/**
  * Generally a 'use' value is used to populate a data query while a 'display'
  * value is used to render UI
  *
  * @param {Object} data, raw data that could be a String, Number or Object
  * @return a JSON object that has both 'display' and 'use' attribute
  **/
	QueryResultData.toDataItem = function (data) {
		return new FacetClass(data);
	};

	/**
  * Create a raw data object with the given use and display value
  *
  * @param u use value
  * @param d display value
  * @return raw data object
  */
	QueryResultData.createDataObject = function (u, d) {
		var dataObj = {};
		if (u) {
			dataObj.u = u;
		}
		if (d) {
			dataObj.d = d;
		}
		return dataObj;
	};

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