import {
  ProtocolRequest,
  ProtocolResponse,
  ProtocolEncoder,
  PacketBody,
  PacketBodyValue,
  ProtocolDecoder,
  ProtocolMessage
} from '@ucap-webmessenger/protocol';

export interface RoomInviteRequest extends ProtocolRequest {
  // 0. 대화방SEQ(s)
  roomSeq: string;
  // 1n. 초대자 userSeq(n)...
  inviteUserSeqs: number[];
}

export interface RoomInviteResponse extends ProtocolResponse {
  // 0. 대화방SEQ(s)
  roomSeq: string;
}

export const encodeRoomInvite: ProtocolEncoder<RoomInviteRequest> = (
  req: RoomInviteRequest
) => {
  const bodyList: PacketBody[] = [];

  bodyList.push({ type: PacketBodyValue.String, value: req.roomSeq });
  for (const userSeq of req.inviteUserSeqs) {
    bodyList.push({ type: PacketBodyValue.Integer, value: userSeq });
  }

  return bodyList;
};

export const decodeRoomInvite: ProtocolDecoder<RoomInviteResponse> = (
  message: ProtocolMessage
) => {
  return {
    roomSeq: message.bodyList[0]
  } as RoomInviteResponse;
};

export const decodeRoomInviteNotification: ProtocolDecoder<
  RoomInviteResponse
> = (message: ProtocolMessage) => {
  return {
    roomSeq: message.bodyList[0]
  } as RoomInviteResponse;
};
