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

import { Observable } from 'rxjs';
import { take, map } from 'rxjs/operators';
import { ProtocolService } from '@ucap-webmessenger/protocol';
import {
  DataUserRequest,
  SSVC_TYPE_QUERY_DATA_USER_REQ,
  SVC_TYPE_QUERY_DATA,
  encodeDataUser,
  DataUserResponse
} from '@ucap-webmessenger/protocol-query';
import { decodeDataUser } from '../protocols/data-user';

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

  public dataUserDaesang(req: DataUserRequest): Observable<DataUserResponse> {
    return this.protocolService
      .call(
        SVC_TYPE_QUERY_DATA,
        SSVC_TYPE_QUERY_DATA_USER_REQ,
        ...encodeDataUser(req)
      )
      .pipe(
        take(1),
        map(res => decodeDataUser(res))
      );
  }
}
