'use strict';

/**
 * Licensed Materials - Property of IBM
 * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2020
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */
define(['../lib/@waca/core-client/js/core-client/ui/core/View', '../lib/@waca/core-client/js/core-client/i18n/Formatter', '../nls/StringResources', 'text!./templates/Labels.template', 'underscore'], function (View, Formatter, stringResources, template, _) {

	/**
  * Display a list of labels. Each label has a name and value.
  */
	var Labels = View.extend({
		templateString: template,

		init: function init(options) {
			Labels.inherited('init', this, arguments);
			this.labels = options.labels;
			this.title = options.title;
			this._formatter = options.formatter || Formatter;
		},

		/**
   * Draws the labels
   */
		render: function render() {
			var labels = [];
			if (this.labels) {
				for (var i = 0; i < this.labels.length; i++) {
					var label = this.labels[i];
					var value = this._getValue(label);
					var stringResourceName = value ? 'toolbarLabel' : 'toolbarNoValueLabel';
					labels.push({
						name: stringResources.get(stringResourceName, { labelName: _.escape(label.name) }),
						value: value
					});
				}
			}

			var sHtml = this.dotTemplate({ title: _.escape(this.title), labels: labels });
			this.$el.addClass('labels');
			this.$el.html(sHtml);
		},

		_getValue: function _getValue(label) {
			var value = label.value;
			var formatSpec = label.formatSpec;
			if (formatSpec && !isNaN(value)) {
				var _formatSpec = formatSpec.validFormatSpec || formatSpec.formatSpec;
				if (_formatSpec) {
					formatSpec = _formatSpec;
				}
				return this._formatter.format(value, formatSpec);
			} else {
				return value;
			}
		}
	});

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