import { StatusCode, JsonAnalization } from '@ucap-webmessenger/api';
import { EventJsonDecoder } from './event-json';

export interface MassTextEventJson {
  statusCode?: StatusCode;
  errorMessage?: string;
  roomSeq?: number;
  massSeq?: number;
  regDate?: string;
  content?: string;
}

export const decodeMassTextEventJson: EventJsonDecoder<MassTextEventJson> = (
  message: string
) => {
  try {
    const json = JsonAnalization.receiveAnalization(message);

    return {
      statusCode: json.StatusCode,
      errorMessage: json.ErrorMessage,
      roomSeq: Number(json.RoomID),
      massSeq: Number(json.EventMassSeq),
      regDate: json.RegDate,
      content: json.Content
    } as MassTextEventJson;
  } catch (e) {
    return {
      statusCode: StatusCode.Fail,
      errorMessage: e.toString()
    } as MassTextEventJson;
  }
};
