export const CONTACT_DOCUMENT_SCOPE_PREFIX = "contact:"; export function buildContactDocumentScope(contactId: string, contactName: string) { return `${CONTACT_DOCUMENT_SCOPE_PREFIX}${encodeURIComponent(contactId)}:${encodeURIComponent(contactName)}`; } export function parseContactDocumentScope(scope: string) { const raw = String(scope ?? "").trim(); if (!raw.startsWith(CONTACT_DOCUMENT_SCOPE_PREFIX)) return null; const payload = raw.slice(CONTACT_DOCUMENT_SCOPE_PREFIX.length); const [idRaw, ...nameParts] = payload.split(":"); const contactId = decodeURIComponent(idRaw ?? "").trim(); const contactName = decodeURIComponent(nameParts.join(":") ?? "").trim(); if (!contactId) return null; return { contactId, contactName, }; } export function formatDocumentScope(scope: string) { const linked = parseContactDocumentScope(scope); if (!linked) return scope; return linked.contactName ? `Contact ยท ${linked.contactName}` : "Contact document"; } export function isDocumentLinkedToContact( scope: string, contact: { id: string; name: string } | null | undefined, ) { if (!contact) return false; const linked = parseContactDocumentScope(scope); if (!linked) return false; if (linked.contactId) return linked.contactId === contact.id; return Boolean(linked.contactName && linked.contactName === contact.name); }