/*
 * 当前会话头部模块：统一刷新标题、备注信息和输入状态。
 * 作者: @ccfork
 */
import { els, state } from './state.js';
import { sessionLabel } from './utils.js';

function isChatTyping(session) {
    const typingUntil = Number(session.typing_until || 0);
    return !!session.chat_is_typing || typingUntil >= Math.floor(Date.now() / 1000);
}

export function updateCurrentSessionHeader() {
    const session = state.currentSession;
    if (!session) {
        els.currentTitle.textContent = '选择一个用户';
        els.currentMeta.textContent = '左侧点开后就可以写小纸条';
        els.presenceLine.textContent = '状态会在这里冒泡～';
        return;
    }

    const remark = String(session.remark || '').trim();
    const id = session.user_id || session.id;
    const metaBits = [];
    const stateBits = [];

    els.currentTitle.textContent = remark || sessionLabel(session);
    metaBits.push(remark ? `ID ${id}` : '未备注');
    metaBits.push(`IP ${session.ip || '-'}`);

    if (isChatTyping(session)) {
        metaBits.push('输入中');
        stateBits.push('对方正在输入～');
    }

    els.currentMeta.textContent = metaBits.join(' · ');
    els.presenceLine.textContent = stateBits.join(' · ') || '小纸条通道很安静～';
}
