import { toggleSidebar, dom } from './state.js';
import { initSessions } from './sessions.js';
import { openNewChatWizard, initNewChatWizard } from './newChatWizard.js';
import { openChatSettings, initChatSettings } from './chatSettings.js';
import { initContextEditor } from './contextEditor.js';
import { loadPersonas, initPersonaModals } from './personas.js';
import { sendMessage, clearHistory, initQuestPanel } from './chat.js';

document.getElementById('sidebarToggle').addEventListener('click', () => {
    const open = toggleSidebar();
    document.getElementById('sidebar').classList.toggle('collapsed', !open);
});

document.getElementById('newChatBtn').addEventListener('click', openNewChatWizard);
document.getElementById('chatSettingsBtn')?.addEventListener('click', openChatSettings);

dom.inputEl.addEventListener('input', () => {
    dom.inputEl.style.height = 'auto';
    dom.inputEl.style.height = dom.inputEl.scrollHeight + 'px';
});

dom.inputEl.addEventListener('keydown', (e) => {
    if (e.key === 'Enter' && !e.shiftKey) {
        e.preventDefault();
        sendMessage();
    }
});

dom.sendBtn.addEventListener('click', sendMessage);
dom.clearBtn.addEventListener('click', clearHistory);

dom.systemBlobToggle?.addEventListener('click', () => {
    const hidden = dom.systemBlobContent.classList.toggle('hidden');
    dom.systemBlobToggle.textContent = hidden ? 'Показать' : 'Скрыть';
});

initPersonaModals();
initNewChatWizard();
initChatSettings();
initContextEditor();
initQuestPanel();
await initSessions();
loadPersonas();
