/**
 * Mention parsing (§4.7 feature 4.7) — exists exactly once and is imported by
 * both the composer (client) and the write path (server), per ADR-002's
 * shared-logic rationale. Server-persisted `message_mentions` rows are what
 * drive mention badges (invariant I6); this parser produces the candidates
 * that path persists, never the badge itself.
 */

export type ParsedMention =
  | { kind: "user"; handle: string }
  | { kind: "group"; handle: string }
  | { kind: "here" }
  | { kind: "channel" }
  | { kind: "everyone" };

const MENTION_PATTERN = /@(here|channel|everyone|[a-zA-Z0-9._-]{1,80})/g;

export function parseMentions(text: string): ParsedMention[] {
  const mentions: ParsedMention[] = [];
  for (const match of text.matchAll(MENTION_PATTERN)) {
    const token = match[1]!;
    if (token === "here") mentions.push({ kind: "here" });
    else if (token === "channel") mentions.push({ kind: "channel" });
    else if (token === "everyone") mentions.push({ kind: "everyone" });
    else mentions.push({ kind: "user", handle: token });
  }
  return mentions;
}
