import { fetchContentSave, fetchRevalidate, fetchNodeUpdate, fetchNodeCreation, fetchNodeMove, fetchNodeRename, fetchNodeRemoval, sendRevalidateAllBeacon, fetchLastUpdated, fetchFileManagerList, createFileManagerFolder, removeFileManagerItem, renameFileManagerItem, rotateFileManagerImage, moveFileManagerItem, cropFileManagerItem, fileManagerMoveList, uploadFileManagerItem, fetchFileManagerStorageStats, fetchTimestamp, } from "./fetcher.js";
import { cleanupEditor } from "./cleanup.js";
import t from "../i18n/index.js";
const actions = {
    saveNode: fetchNodeUpdate,
    create: fetchNodeCreation,
    move: fetchNodeMove,
    rename: fetchNodeRename,
    remove: fetchNodeRemoval,
};
const fileManagerActions = {
    getList: fetchFileManagerList,
    getStorageStats: fetchFileManagerStorageStats,
    createFolder: createFileManagerFolder,
    removeItem: removeFileManagerItem,
    renameItem: renameFileManagerItem,
    rotateImage: rotateFileManagerImage,
    moveItem: moveFileManagerItem,
    cropItem: cropFileManagerItem,
    moveList: fileManagerMoveList,
    uploadItem: uploadFileManagerItem,
};
export async function saveNavContent(action, node) {
    const result = await actions[action](node);
    window.sessionStorage.setItem("eden_revalidation_needed", "true");
    return result;
}
export async function saveEditors({ lang, path, startedAt, ignoreFixedEditors = false, }) {
    try {
        const editorElements = Array.from(document.querySelectorAll("[data-eden-editor]"));
        const hasFixedEditors = editorElements.some((element) => element.getAttribute("data-eden-fixed-content") === "true" &&
            element.hasAttribute("data-eden-modified"));
        const editors = editorElements
            .map((element) => {
            const content = cleanupEditor(element.innerHTML);
            return {
                code: element.getAttribute("data-eden-editor"),
                content,
                fixed: element.getAttribute("data-eden-fixed-content") === "true",
            };
        })
            .filter((editor) => (ignoreFixedEditors ? !editor.fixed : true));
        const { error } = await fetchContentSave(lang, path, editors, startedAt);
        if (error) {
            return { error };
        }
        if (hasFixedEditors) {
            sendRevalidateAllBeacon(lang);
            editorElements.forEach((element) => element.removeAttribute("data-eden-modified"));
        }
        fetchRevalidate(lang, path);
        return { status: "ok" };
    }
    catch (e) {
        console.error(e);
        return { error: t("error.save-retry") };
    }
}
export async function checkLastUpdated(path) {
    const { lang, langPrefix } = window.EdenConfig;
    try {
        return fetchLastUpdated(lang, langPrefix, path);
    }
    catch {
        console.error(t("console.last-updated-check-error"));
        return {};
    }
}
export async function getTimestamp() {
    return fetchTimestamp();
}
export async function getFilesList(path) {
    return fileManagerActions.getList(path);
}
export async function getFilesStorageStats() {
    return fileManagerActions.getStorageStats();
}
export async function createFolder(name, path) {
    return fileManagerActions.createFolder(name, path);
}
export async function removeItem(path, paths) {
    return fileManagerActions.removeItem(path, paths);
}
export async function renameItem(file, name) {
    return fileManagerActions.renameItem(file, name);
}
export async function cropItem(path, left, top, width, height, targetWidth, targetHeight, only) {
    return fileManagerActions.cropItem(path, left, top, width, height, targetWidth, targetHeight, only);
}
export async function moveList(paths) {
    return fileManagerActions.moveList(paths);
}
export async function moveItem(path, paths, target) {
    return fileManagerActions.moveItem(path, paths, target);
}
export async function rotateImage(path) {
    return fileManagerActions.rotateImage(path);
}
export async function uploadItem(payload) {
    return fileManagerActions.uploadItem(payload);
}
