import { ipcRenderer } from 'electron';
import * as $ from "../../third_party/js/jquery-3.5.1.js";
let recent_files_div = $("#recent-files")

async function showRecentFiles() {
	let recent_files = await ipcRenderer.invoke("get-store-value", "recent_files");
	recent_files_div.empty();
	if (recent_files.length === 0) {
		recent_files_div.css("display", "none");
		$("#splash").css("width", "100%")
	} else {
		for (let file of recent_files) {
			let recent_file_link = $("<button>")
				.addClass("list-group-item list-group-item-action d-flex justify-content-between align-items-center")
				.text(file.replace(/^.*[\\\/]/, ''))
				.attr({"data-bs-toggle": "tooltip", "data-bs-placement": "bottom", "title": file})
				.on("click", () => {
					ipcRenderer.send("open", file);
				})
				.append($("<a>")
					.addClass("btn-close")
					.on("click", () => {
						recent_file_link.remove();
						removeRecentFile(file);
					}));
			recent_files_div.append(recent_file_link);
		}
	}
}

async function removeRecentFile(file) {
	await ipcRenderer.invoke("remove-recent-file", file);
	showRecentFiles();
}

showRecentFiles();

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

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