import * as lib from '/web/js/api/libraries.js';
import { url_params } from '/web/js/lib/search_params.js'
const library_listing = (await lib.ls(url_params.get("lib"), url_params.get("path"))).Listing
const container = document.getElementById("library-file-list-container");
const template = document.getElementById("library-generic-item-template");
container.innerHTML = "";
//non-fatal error if listing is empty
for (const file_entry of library_listing) {
const clone = template.content.cloneNode(true);
const link = clone.querySelector(".library-generic-item-href");
if (file_entry.Type === "directory") {
link.textContent = `
${file_entry.Name}`
//TODO/BUGFIX: occurence of double slash when referencing the path as root, replace with proper bugfix
if (url_params.get("path").endsWith("/")) {
link.href = `/web/library_viewer?lib=${url_params.get("lib")}&path=${url_params.get("path")}${file_entry.Name}`
} else {
link.href = `/web/library_viewer?lib=${url_params.get("lib")}&path=${url_params.get("path")}/${file_entry.Name}`
}
}
if (file_entry.Type === "video") {
link.textContent = ` ${file_entry.Name}`
link.href = `/web/video_player?lib=${url_params.get("lib")}&path=${url_params.get("path")}/${file_entry.Name}`
}
//TODO: add generic file listings
container.appendChild(clone);
}