// ==================== ROUTING FUNCTIONS =============================
// setup a dict of variables so that plain text can be entered in URL
function getVarDict() {
	const dict = {
		'AOD': 'OD550_DUST',
		'Concentration': 'SCONC_DUST',
		'Dry deposition': 'DUST_DEPD', 
		'Wet deposition': 'DUST_DEPW', 
		'Load': 'DUST_LOAD',
		'Extinction': 'DUST_EXT_SFC'
	}
	return dict
};

//convert date from DD MON YYYY to YYYYMMDD
function getDate() {
	const dict = {
		'Jan': '01',
		'Feb': '02',
		'Mar': '03', 
		'Apr': '04', 
		'May': '05', 
		'Jun': '06',
		'Jul': '07', 
		'Aug': '08', 
		'Sep': '09', 
		'Oct': '10', 
		'Nov': '11', 
		'Dec': '12'
	};
 	date = document.getElementById('date').value;
	splitDate = date.split(' ');
	month = dict[splitDate[1]];
	return '&date=' + splitDate[2] + month + splitDate[0];
}

function getVar() {
	//get the variable value
	curvar = document.querySelector('#react-select-2--value-item').innerHTML;
	const varDict = getVarDict(); 
	return 'var=' + varDict[curvar] + '&';	
}

function getModels(){
		// get models checked
		const data = [...document.querySelectorAll('.custom-control-input:checked')].map(e => e.value);
		data.forEach(function(item, index){
			this[index] = 'model=' + item
		}, data);		
		return data.join('&');
}

// CREATE OUTPUT URL  
$(document).ready(function () {
    $(document).on('click', "#models-apply", function () {
		curvar = getVar()
		models = getModels()
		outputDate = getDate();
		//convert to the output URL
		url = "/dashboard/?" + curvar + models + outputDate;
		window.history.pushState("object or string", "Title", url);
	})	
});

