const Helper = {
	toast: function (title, time, msg, type = 'bg-warning', delay = 2500) {
		$('#liveToast-type').addClass(type);
		document.getElementById('liveToast-title').innerText = title;
		document.getElementById('liveToast-time').innerText = time;
		document.getElementById('liveToast-msg').innerText = msg;
		(new bootstrap.Toast(document.getElementById('liveToast'), { delay })).show();
	},
	decodeHTMLEntities: function (str) {
		if (str && typeof str === 'string') {
			// strip script/html tags
			str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
			str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
			this.element.innerHTML = str;
			str = this.element.textContent;
			this.element.textContent = '';
		}

		return str;
	},
	titleCase: function (str) {
		var splitStr = str.toLowerCase().split(' ');
		for (var i = 0; i < splitStr.length; i++) {
			// You do not need to check if i is larger than splitStr length, as your for does that for you
			// Assign it back to the array
			splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
		}
		// Directly return the joined string
		return splitStr.join(' ');
	},
	rangeToday: function () {
		return {
			start: moment().format('DD-MM-YYYY'),
			end: moment().format('DD-MM-YYYY'),
		}
	},
	rangeWeek: function () {
		return {
			start: moment().subtract(7, 'day').format('DD-MM-YYYY'),
			end: moment().format('DD-MM-YYYY'),
		}
	},
	rangeMonth: function () {
		return {
			start: moment().subtract(1, 'month').format('DD-MM-YYYY'),
			end: moment().format('DD-MM-YYYY'),
		}
	},
	splitEvery4Char: function (str) {
		if (str) {
			str = '' + str;
			let splitEvery4Char = str.match(/.{1,4}/g)
			return splitEvery4Char.join(' ')
		}
		return '';
	},
	getCookie: function (name) {
		var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
		if (match) {
			return match[2];
		}
		else {
			return null;
		}
	},
	shortenText: function (text, max) {
		return text && text.length > max ? text.slice(0, max).split(' ').slice(0, -1).join(' ') : text
	},
	initQueryString: function () {
		// https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
		const params = new Proxy(new URLSearchParams(window.location.search), {
			get: (searchParams, prop) => searchParams.get(prop),
		});
		return params;
	},
	getPercentRange: function (crntVal, min, max) {
		return (crntVal - min) / (max - min) * 100;
	},
	getIndexReversedSequence: function(normalIdx, lastIdx) {
		return Math.abs(normalIdx - lastIdx);
	},
	// https://stackoverflow.com/questions/1184624/convert-form-data-to-javascript-object-with-jquery
	objectifyForm: function(formArray) {
    //serialize data function
    var returnArray = {};
    for (var i = 0; i < formArray.length; i++){
			if (formArray[i]['name'].indexOf('[]') > -1) { // dynamic data type
				let key = formArray[i]['name'].replace('[]', '');
				if (typeof returnArray[key] === 'undefined') {
					returnArray[key] = [formArray[i]['value']];
				} else {
					returnArray[key].push(formArray[i]['value']);
				}
			} else { // primitive data type
				returnArray[formArray[i]['name']] = formArray[i]['value'];
			}
    }
    return returnArray;
}
};