export let sessionId = localStorage.getItem('chat_session_id') || null;
/** Persona bound to the active session (from server, not global preset). */
export let currentPersona = 'default';
export let sidebarOpen = true;
export let rpgEnabled = false;

const NEW_CHAT_PERSONA_KEY = 'new_chat_persona_id';

export function toggleSidebar() { sidebarOpen = !sidebarOpen; return sidebarOpen; }

export function getNewChatDefaultPersona() {
    return localStorage.getItem(NEW_CHAT_PERSONA_KEY)
        || localStorage.getItem('persona_id')
        || 'default';
}

export function setNewChatDefaultPersona(id) {
    const pid = id || 'default';
    localStorage.setItem(NEW_CHAT_PERSONA_KEY, pid);
}

export function setSessionId(id) {
    sessionId = id;
    if (id) localStorage.setItem('chat_session_id', id);
}

export function setCurrentPersona(id) {
    currentPersona = id || 'default';
}

export function setRpgEnabled(v) { rpgEnabled = !!v; }

export const dom = {
    messagesEl: document.getElementById('messages'),
    inputEl: document.getElementById('input'),
    sendBtn: document.getElementById('sendBtn'),
    clearBtn: document.getElementById('clearBtn'),
    sessionList: document.getElementById('sessionList'),
    headerTitle: document.getElementById('headerTitle'),
    emptyState: document.getElementById('emptyState'),
    affinityDisplay: document.getElementById('affinityDisplay'),
    statsDisplay: document.getElementById('statsDisplay'),
    rpgBadge: document.getElementById('rpgBadge'),
    systemBlob: document.getElementById('systemBlob'),
    systemBlobContent: document.getElementById('systemBlobContent'),
    systemBlobToggle: document.getElementById('systemBlobToggle'),
    systemBlobRefresh: document.getElementById('systemBlobRefresh'),
};
