import { BinaryWriter } from "../binary/writer";

export enum WriteKind {
  Offer,
  Answer,
  IceCandidate,
}
export type WriteEvent = {
  kind: WriteKind.IceCandidate,
  to: string,
  candidate: RTCIceCandidate
} | {
  kind: WriteKind.Offer,
  to: string,
  offerSdp: string
} | {
  kind: WriteKind.Answer,
  to: string,
  answerSdp: string
}
export function writeEvent(writer: BinaryWriter, event: WriteEvent) {
  writer.writeUInt8(event.kind);
  writer.writeFixedString(36, event.to);
  switch (event.kind) {
    case WriteKind.Offer: writer.writePrefixedString(event.offerSdp); return;
    case WriteKind.Answer: writer.writePrefixedString(event.answerSdp); return;
    case WriteKind.IceCandidate: writer.writePrefixedString(JSON.stringify(event.candidate.toJSON())); return;
    default: throw new Error("kind not good");
  }
}
