"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initDashboardView = initDashboardView; const api_js_1 = require("../services/api.js"); const store_js_1 = require("../state/store.js"); const toast_js_1 = require("../ui/toast.js"); const overview_js_1 = require("./overview.js"); const index_js_1 = require("./tickets/index.js"); const index_js_2 = require("./modules/index.js"); const index_js_3 = require("./events/index.js"); const index_js_4 = require("./admin/index.js"); const settings_js_1 = require("./settings.js"); let overviewInterval = null; let ticketsInterval = null; async function populateGuildSelect() { const select = document.getElementById('guildSelect'); const cfg = (0, store_js_1.getConfig)(); if (!select || !cfg) return; select.innerHTML = ``; try { const data = await api_js_1.api.guilds(); select.innerHTML = ''; data.guilds.forEach((g) => { const opt = document.createElement('option'); opt.value = g.id; opt.textContent = g.name; if (g.id === cfg.initialGuildId) opt.selected = true; select.appendChild(opt); }); const current = select.value || cfg.initialGuildId || data.guilds[0]?.id; (0, store_js_1.setState)({ guildId: current || undefined }); select.value = current || ''; } catch (err) { console.error(err); (0, toast_js_1.showToast)('Guilds konnten nicht geladen werden', true); } } function registerGuildChange() { const select = document.getElementById('guildSelect'); if (!select) return; select.addEventListener('change', () => { const guildId = select.value; (0, store_js_1.setState)({ guildId }); refreshSections(); }); } async function refreshSections() { const { guildId } = (0, store_js_1.getState)(); if (!guildId) return; await (0, overview_js_1.renderOverview)(guildId); await (0, index_js_1.initTicketsSection)(guildId); await (0, index_js_2.initModulesSection)(guildId); await (0, settings_js_1.renderSettingsSection)(guildId); await (0, index_js_3.initEventsSection)(guildId); const cfg = (0, store_js_1.getConfig)(); if (cfg?.isAdmin) { await (0, index_js_4.initAdminSection)(guildId); } } function setupPolling() { const { guildId } = (0, store_js_1.getState)(); if (overviewInterval) window.clearInterval(overviewInterval); if (ticketsInterval) window.clearInterval(ticketsInterval); overviewInterval = window.setInterval(() => { const current = (0, store_js_1.getState)().guildId; if (current) (0, overview_js_1.renderOverview)(current); }, 10000); ticketsInterval = window.setInterval(() => { const current = (0, store_js_1.getState)().guildId; if (current) (0, index_js_1.initTicketsSection)(current); }, 12000); } function initDashboardView() { const cfg = (0, store_js_1.getConfig)(); const logoutBtn = document.getElementById('logoutBtn'); if (logoutBtn && cfg) { logoutBtn.addEventListener('click', () => (window.location.href = `${cfg.baseAuth}/logout`)); } populateGuildSelect().then(() => { registerGuildChange(); refreshSections(); setupPolling(); }); }