export type ChatMessageDeepLinkInput = Readonly<{ tenantSlug: string; chatId: string; messageId: string; parentMessageId?: string | null; }>; /** Canonical exact-message target used by notifications, pins, and search. */ export function buildChatMessageDeepLink(input: ChatMessageDeepLinkInput): string { const tenantSlug = encodeURIComponent(requiredSegment(input.tenantSlug, "tenantSlug")); const chatId = encodeURIComponent(requiredSegment(input.chatId, "chatId")); const messageId = encodeURIComponent(requiredSegment(input.messageId, "messageId")); const parentMessageId = optionalSegment(input.parentMessageId); const threadPath = parentMessageId ? `/thread/${encodeURIComponent(parentMessageId)}` : ""; return `/${tenantSlug}/chat/${chatId}${threadPath}?message=${messageId}#message-${messageId}`; } function requiredSegment(value: string, name: string): string { const normalized = value.trim(); if (!normalized) throw new Error(`chat deep-link ${name} is required`); return normalized; } function optionalSegment(value: string | null | undefined): string | null { if (value === null || value === undefined) return null; return requiredSegment(value, "parentMessageId"); }