import type { ChatMessage } from "@/components/chat/ChatMessageCard";

export function dedupeChatMessages(messages: ChatMessage[]): ChatMessage[] {
  const seen = new Set<string>();
  const result: ChatMessage[] = [];

  for (const m of messages) {
    if (!m) continue;
    const idKey =
      m._id && /^[a-f\d]{24}$/i.test(m._id)
        ? m._id
        : `${m.senderId}|${m.content}|${m.file}|${m.createdAt}`;
    if (seen.has(idKey)) continue;
    seen.add(idKey);
    result.push(m);
  }

  return result;
}
