import {
  ProtocolDecoder,
  ProtocolMessage,
  BodyStringDivider,
  ProtocolNotification,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';
import { SenderInfo } from '../models/sender-info';

export interface UmgDeleteNotiNotification extends ProtocolNotification {
  /** 송신자SEQ(n) */
  senderSeq: number;
  /** { 송신자정보 } */
  senderInfo: SenderInfo;
  /** keyID(s) */
  keyId: string;
}

export const decodeUmgDeleteNotiNotification: ProtocolDecoder<UmgDeleteNotiNotification> = (
  message: ProtocolMessage
) => {
  let senderInfo: SenderInfo;
  if (message.bodyList.length > 1) {
    const info = message.bodyList[1].split(BodyStringDivider);
    senderInfo = {
      seq: Number(info[0]),
      name: info[1],
      profileImageFile: info[2],
      grade: info[3]
    };
  }
  return decodeProtocolMessage(message, {
    /** 송신자SEQ(n) */
    senderSeq: Number(message.bodyList[0]),
    /** { 송신자정보 } */
    senderInfo,
    /** keyID(s) */
    keyId: message.bodyList[2]
  } as UmgDeleteNotiNotification);
};
