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

export interface PlanEventJson {
  planSeq?: number;
  title?: string;
  contents?: PlanContentType | string;
  date?: string;
  endDate?: string;
  active?: boolean;
}
export enum PlanContentType {
  New = 'PLAN_CONTENTS_NEW',
  Update = 'PLAN_CONTENTS_UPDATE',
  Delete = 'PLAN_CONTENTS_DELETE'
}

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