const exportButton = document.getElementById("export-text");
const imageElement = document.getElementById("background-image");

const checkJSON = document.getElementById("checkJSON");
const checkTEXT = document.getElementById("checkText");

let fileUrl = imageElement.src;
let fileName = fileUrl.slice(fileUrl.lastIndexOf("/") + 1, fileUrl.length);
let fileTitle = fileName.slice(0, fileName.indexOf(".")) || "export";


exportButton.addEventListener("click", (e) => {
    console.log(checkJSON.value, checkTEXT.value)
	if (checkJSON.checked) exportJSON();
	if (checkTEXT.checked) exportText();
});

function exportText() {
	let text = document.querySelector("ol").innerText;
	let title = fileTitle + ".txt";
	download(text, title, "text/plain;charset=utf-8");
}

function exportJSON() {
	let title = fileTitle + ".json";
	download(JSON.stringify(labelsObj), title, "application/json");
}

// https://stackoverflow.com/questions/13405129/javascript-create-and-save-file
// Thank you Kanchu!
// Function to download data to a file
function download(data, filename, type) {
	var file = new Blob([data], { type: type });
	if (window.navigator.msSaveOrOpenBlob)
		// IE10+
		window.navigator.msSaveOrOpenBlob(file, filename);
	else {
		// Others
		var a = document.createElement("a"),
			url = URL.createObjectURL(file);
		a.href = url;
		a.download = filename;
		document.body.appendChild(a);
		a.click();
		setTimeout(function () {
			document.body.removeChild(a);
			window.URL.revokeObjectURL(url);
		}, 0);
	}
}
