import { PacketBody } from './packet';

export interface ProtocolRequest {
  _id?: string;
}

export interface ProtocolFromServer {
  _id?: string;
  SVC_TYPE?: number;
  SSVC_TYPE?: number;
  SENDER_SEQ?: number;
}

// tslint:disable-next-line: no-empty-interface
export interface ProtocolResponse extends ProtocolFromServer {}

// tslint:disable-next-line: no-empty-interface
export interface ProtocolStream extends ProtocolFromServer {}

// tslint:disable-next-line: no-empty-interface
export interface ProtocolNotification extends ProtocolFromServer {}

export interface ProtocolMessage {
  serviceType: number;
  subServiceType: number;
  senderSeq: number;
  bodyList: any[];
}

export type ProtocolEncoder<REQ> = (req: REQ) => PacketBody[];

export type ProtocolDecoder<RES extends ProtocolFromServer> = (
  message: ProtocolMessage
) => RES;

export function decodeProtocolMessage<RES extends ProtocolFromServer>(
  message: ProtocolMessage,
  res: any
): RES {
  return {
    SVC_TYPE: message.serviceType,
    SSVC_TYPE: message.subServiceType,
    SENDER_SEQ: message.senderSeq,
    ...res
  } as RES;
}
