/* Presentational helpers shared by the internal inbox. No network or mutations. */
window.BmcMessageUI = (() => {
const escape = value => String(value ?? '').replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
const initials = name => String(name || '?').trim().split(/\s+/).slice(0,2).map(p=>p[0]).join('').toUpperCase();
const time = value => { const d=new Date(value); return value && !Number.isNaN(d.getTime()) ? d.toLocaleTimeString('da-DK',{hour:'2-digit',minute:'2-digit'}) : ''; };
function message(m) {
const own=!!m.is_own, phone=m.message_kind==='phone';
const number=String(m.callback_phone || '').replace(/[^+0-9*#,;]/g,'');
const person=m.caller_name || m.contact_name || 'Telefonbesked';
const contact=Number(m.contact_id)>0?` ${escape(m.contact_name || 'Åbn kontakt')}`:'';
const replyId=own?Number(m.recipient_user_id):Number(m.sender_user_id);
const stamp=time(m.created_at);
const status=own?(m.is_acknowledged?'Bekræftet læst':m.is_read?'Læst':'Sendt'):'';
const phoneCard=phone?`
TELEFONBESKED${escape(person)}${m.callback_phone?`${escape(m.callback_phone)}`:''}
${number?`
Ring tilbage`:''}
`:'';
return `${own?'Dig':escape(m.from || 'Kollega')}${stamp?``:''}${m.is_unread?'':''}
${phoneCard}
${escape(m.text)}
${contact}
${status?` ${status}`:''}${m.requires_manual_ack && !m.is_acknowledged?'Læsebekræftelse ønskes':''}${!own && m.requires_manual_ack && !m.is_acknowledged?``:''}${replyId>0?``:''}
`;
}
function thread(t, active) {
const last=t.items?.[t.items.length-1];
const preview=last?`${last.message_kind==='phone'?'Telefonbesked · ':''}${last.text || ''}`:'Start en samtale';
return `${t.partnerUserId?escape(initials(t.label)):''}${escape(t.label)}${escape(preview)}${t.unread?`${Number(t.unread)}`:''}`;
}
return {message,thread,escape,initials};
})();