import { Injectable } from '@angular/core';

import { Observable } from 'rxjs';
import { map, take } from 'rxjs/operators';

import { ProtocolService } from '@ucap-webmessenger/protocol';

import {
  ConnResponse,
  ConnRequest,
  encodeConn,
  decodeConn
} from '../protocols/conn';
import { SVC_TYPE_INNER, SSVC_TYPE_CONN_REQ } from '../types/service';

@Injectable({
  providedIn: 'root'
})
export class InnerProtocolService {
  constructor(private protocolService: ProtocolService) {}

  public conn(req: ConnRequest): Observable<ConnResponse> {
    return this.protocolService
      .call(SVC_TYPE_INNER, SSVC_TYPE_CONN_REQ, ...encodeConn(req))
      .pipe(
        take(1),
        map(res => decodeConn(res))
      );
  }
}
