// retoor <retoor@molodetz.nl>

import { Http } from "./Http.js";

export class MediaGallery {
    constructor() {
        document.addEventListener("click", (e) => {
            const btn = e.target.closest("[data-media-delete]");
            if (!btn) return;
            e.preventDefault();
            this.remove(btn);
        });
    }

    async remove(btn) {
        const uid = btn.dataset.mediaDelete;
        const tile = btn.closest("[data-media-tile]");
        try {
            await Http.send(`/media/${uid}/delete`);
            if (tile) {
                tile.classList.add("media-removing");
                setTimeout(() => tile.remove(), 220);
            }
            if (window.app && window.app.toast) {
                window.app.toast.show("Media deleted");
            }
        } catch (err) {
            if (window.app && window.app.toast) {
                window.app.toast.show(err.message, { type: "error" });
            }
        }
    }
}

window.MediaGallery = MediaGallery;
