window.AppConfig = window.AppConfig || { ready: false }; // =============================================== // WAIT FOR CONFIG // =============================================== function waitForConfig() { return new Promise(resolve => { if (!window.AppConfig) { window.AppConfig = { ready: false }; } if (window.AppConfig.ready) { return resolve(); } window.addEventListener("config:ready", resolve, { once: true }); }); } // =============================================== // API WRAPPER // =============================================== async function apiGet(url) { const token = localStorage.getItem("token"); const res = await fetch(url, { headers: { "Authorization": "Bearer " + token } }); const text = await res.text(); try { return JSON.parse(text); } catch { console.error("❌ Risposta non JSON:", text); throw new Error("Risposta non valida dal server"); } } // =============================================== // GET ALL PHOTOS (FULL LOAD) // =============================================== async function getAllPhotos(user) { await waitForConfig(); return apiGet(`/photos?user=${encodeURIComponent(user)}`); } // =============================================== // GET PHOTO BY ID // =============================================== async function getPhotoById(id) { await waitForConfig(); const payload = parseJwt(localStorage.getItem("token")); const user = payload?.name || "Common"; return apiGet(`/photos/byIds?id=${encodeURIComponent(id)}&user=${encodeURIComponent(user)}`); } // =============================================== // GET CHANGES (INCREMENTAL SYNC) // =============================================== async function getChanges(since, user) { await waitForConfig(); return apiGet(`/photos/changes?since=${encodeURIComponent(since)}&user=${encodeURIComponent(user)}`); }