import { FILE_MANAGER_EXTENSIONS } from "../../services/fetcher.js";
import { getFilesList, getFilesStorageStats, uploadItem, } from "../../services/api.js";
export function parseFileManagerMetadata(metadata) {
    if (!metadata)
        return;
    const decodedMetadata = atob(Array.from(metadata).reverse().join(""));
    return JSON.parse(decodeURIComponent(decodedMetadata));
}
export function encodeFileManagerCropMetadata(params) {
    return Array.from(btoa(encodeURIComponent(JSON.stringify(params))))
        .reverse()
        .join("");
}
export function buildFileManagerImageSrc(edenApi, site, path) {
    const rand = crypto.randomUUID();
    return edenApi.concat("/api/files/image/", site, path, "?r=", rand);
}
export async function loadFileManagerData(currentPath) {
    const files = await getFilesList(currentPath);
    const storage = await getFilesStorageStats();
    return { files, storage };
}
export async function uploadFilesToCurrentPath({ site, currentPath, files, extensions = FILE_MANAGER_EXTENSIONS, }) {
    for await (const file of files) {
        const formData = new FormData();
        formData.append("site", site);
        formData.append("path", currentPath);
        formData.append("extensions", extensions.join(","));
        formData.append("files", file);
        await uploadItem(formData);
    }
}
export function resolvePreviousBreadcrumbPath(breadcrumb) {
    if (!breadcrumb)
        return;
    const previous = breadcrumb[breadcrumb.length - 2] || breadcrumb[0];
    return previous.path;
}
