"use strict";

// document.addEventListener('DOMContentLoaded', () => {

var updateQueryString = function updateQueryString(key, value, url) {
	if (!url) url = window.location.href;
	var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),
	    hash;

	if (re.test(url)) {
		if (typeof value !== 'undefined' && value !== null) return url.replace(re, '$1' + key + "=" + value + '$2$3');else {
			hash = url.split('#');
			url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
			if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += '#' + hash[1];
			return url;
		}
	} else {
		if (typeof value !== 'undefined' && value !== null) {
			var separator = url.indexOf('?') !== -1 ? '&' : '?';
			hash = url.split('#');
			url = hash[0] + separator + key + '=' + value;
			if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += '#' + hash[1];
			return url;
		} else return url;
	}
};
var generateNewFilter = function generateNewFilter(value) {
	var updatedUrl = void 0;
	var searchTerm = getUrlParameter("searchterm");
	var filter = getUrlParameter("filter");
	console.log(value, filter);
	if (searchTerm != '') {
		updatedUrl = updateQueryString("searchterm", searchTerm, location.href);
	}
	if (value === filter) {
		updatedUrl = updateQueryString("filter", "", updatedUrl);
	} else {
		updatedUrl = updateQueryString("filter", value, updatedUrl);
	}
	location.href = updatedUrl;
};
var addLoader = function addLoader() {
	document.getElementById('supportLoader').classList.add('active');
};
var styleFilters = function styleFilters(arr) {
	var urlFilters = getUrlParameter("filter");
	if (urlFilters.length <= 0) {
		return;
	}
	urlFilters = urlFilters.split(',');

	arr.map(function (f) {
		urlFilters.map(function (url) {
			if (f.attributes['data-filter'].value === url) {
				f.classList.add('active');
				f.checked = true;
			}
		});
	});
};
var addCheckboxListeners = function addCheckboxListeners(checkboxes) {
	checkboxes.map(function (box) {
		box.addEventListener('click', function () {
			generateNewFilter(box.attributes['data-filter'].value);
			addLoader();
		});
	});
};
var checkboxes = Array.from(document.querySelectorAll('[data-filter]'));
addCheckboxListeners(checkboxes);
styleFilters(checkboxes);

// })