import type { IMessage } from '@stomp/stompjs';

/** STOMP MESSAGE body — 텍스트·바이너리(UTF-8 JSON) 모두 처리 */
export function stompMessageBody(msg: IMessage): string {
  if (msg.body && msg.body.length > 0) return msg.body;
  const bin = msg.binaryBody;
  if (bin && bin.length > 0) {
    return new TextDecoder('utf-8').decode(bin);
  }
  return '';
}

export function parseStompJson<T>(msg: IMessage): T | null {
  const raw = stompMessageBody(msg);
  if (!raw) return null;
  try {
    return JSON.parse(raw) as T;
  } catch {
    return null;
  }
}
