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

export interface PlanEventJson {
  planSeq?: number;
  title?: string;
  contents?: string;
  date?: string;
  endDate?: string;
  active?: boolean;
}

export const decodePlanEventJson: EventJsonDecoder<PlanEventJson> = (
  message: string
) => {
  try {
    const json = JsonAnalization.receiveAnalization(message);
    return {
      planSeq: json.planSeq,
      title: json.title,
      contents: json.contents,
      date: json.date,
      endDate: json.endDate,
      chat: json.chat,
      active: !!json.activeYn && 'Y' === json.activeYn ? true : false
    } as PlanEventJson;
  } catch (e) {
    return {} as PlanEventJson;
  }
};
