/**
 * @package     Reporter for VirtueMart
 * 
 * @copyright   Copyright (C) 2012-2021 VirtuePlanet Services LLP. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 * @author      Abhishek Das <info@virtueplanet.com> - http://www.virtueplanet.com
 */

var VMReporter = window.VMReporter || {};

String.prototype.str_replace = function(find, replace) {
	var replaceString = this;
	for (var i = 0; i < find.length; i++) {
		replaceString = replaceString.replace(find[i], replace[i]);
	}
	return replaceString;
};

(function($, VMReporter) {

	VMReporter.options = {};

	VMReporter.loadOptions = function(e) {
		for (var t in e) {
			this.options[t] = e[t]
		}
		return this
	};

	VMReporter.updateOption = function(e, t) {
		this.options[e.toUpperCase()] = t;
	};

	VMReporter._ = function(e, t) {
		return typeof this.options[e.toUpperCase()] !== "undefined" ? this.options[e.toUpperCase()] : t;
	};

	VMReporter.number_format = function(number, decimals, decPoint, thousandsSep) {
		number = (number + '').replace(/[^0-9+\-Ee.]/g, '')
		var n = !isFinite(+number) ? 0 : +number
		var prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
		var sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep
		var dec = (typeof decPoint === 'undefined') ? '.' : decPoint
		var s = ''

		var toFixedFix = function(n, prec) {
			var k = Math.pow(10, prec)
			return '' + (Math.round(n * k) / k)
				.toFixed(prec)
		}

		// @todo: for IE parseFloat(0.55).toFixed(0) = 0;
		s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.')
		if (s[0].length > 3) {
			s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep)
		}

		if ((s[1] || '').length < prec) {
			s[1] = s[1] || ''
			s[1] += new Array(prec - s[1].length + 1).join('0')
		}

		return s.join(dec)
	};

	VMReporter.currency_format = function(number) {
		var currency = VMReporter._('CURRENCY_FORMAT'),
			decimals = currency.currency_decimal_place,
			decPoint = currency.currency_decimal_symbol,
			thousandsSep = currency.currency_thousands,
			symbol = currency.currency_symbol,
			style = currency.currency_positive_style;

		number = VMReporter.number_format(number, decimals, decPoint, thousandsSep);

		if (style && $.type(style) === 'string') {
			return style.str_replace(['{number}', '{symbol}'], [number, symbol]);
		}

		return number;
	};

})(jQuery, VMReporter);