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

export interface MassTranslationEventJson {
  statusCode?: StatusCode;
  errorMessage?: string;
  translationSeq?: number;
  destLocale?: string;
  roomSeq?: number;
  regDate?: string;
  original?: string;
  translation?: string;
}

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

    return {
      statusCode: json.StatusCode,
      errorMessage: json.ErrorMessage,
      translationSeq: Number(json.EventTransSeq),
      destLocale: json.DestLocale,
      roomSeq: Number(json.RoomID),
      regDate: json.RegDate,
      original: json.Original,
      translation: json.Translation
    } as MassTranslationEventJson;
  } catch (e) {
    return {
      statusCode: StatusCode.Fail,
      errorMessage: e.toString()
    } as MassTranslationEventJson;
  }
};
