//-		DS2 core (c) 2024 Alexander McIlwraith 
//-		Licensed under CC BY-SA 4.0 

function flip(e) {
	let sw = e.currentTarget;
	switch(sw.getAttribute("aria-checked")) {
		case "true": 
			sw.setAttribute("aria-checked", "false");
		break;
		case "false": 
			sw.setAttribute("aria-checked", "true");
		break;
	}
};

module.exports = {
	init: (p = document) => {
		try {
			p.querySelectorAll("[role='switch']").forEach((sw) => {
				sw.innerHTML = "<span></span>";
				sw.setAttribute("aria-checked", "false");
				sw.setAttribute("tabindex", "0");
				sw.addEventListener("click", flip, false);
				sw.addEventListener("keypress", flip, false);
			})
		} catch (e) {
			console.warn("Cannot initialise switches.", e);
		}
	}
}
