const { ipcRenderer } = require('electron');
const fs = require("fs");

async function showRecentFiles() {
	let recent_files = await ipcRenderer.invoke("get-store-value", "recent_files");
	let valid_recent_files = [];
	for (let file of recent_files) {
		if (fs.existsSync(file)) {
			valid_recent_files.push(file);
		}
	}
	if (valid_recent_files.length === 0) {
		$("#recent-files").css("display", "none");
		$("#splash").css("width", "100%")
	} else {
		for (let file of valid_recent_files) {
				let recent_file_link = $("<a>")
				.addClass("btn list-group-item list-group-item-action")
				.text(file.replace(/^.*[\\\/]/, ''))
				.attr({"data-bs-toggle": "tooltip", "data-bs-placement": "bottom", "title": file})
				.on("click", () => {
					ipcRenderer.send("open", file);
				});
			$("#recent-files").append(recent_file_link);
		}
	}
}

showRecentFiles();

$("#create-new").on("click", () => {
	ipcRenderer.send("create-new");
});

$("#open-file").on("click", () => {
	ipcRenderer.send("open");
})
