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

import { Observable } from 'rxjs';
import { map, take } from 'rxjs/operators';
import {
  UserPasswordSetRequest,
  UserPasswordSetResponse,
  encodeUserPasswordSet,
  decodeUserPasswordSet
} from '../protocols/password-set';
import { ProtocolService } from '@ucap-webmessenger/protocol';
import { SVC_TYPE_SERVICE, SSVC_TYPE_USER_PW_SET_REQ } from '../types/service';

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

  public userPasswordSet(
    req: UserPasswordSetRequest
  ): Observable<UserPasswordSetResponse> {
    return this.protocolService
      .call(
        SVC_TYPE_SERVICE,
        SSVC_TYPE_USER_PW_SET_REQ,
        ...encodeUserPasswordSet(req)
      )
      .pipe(
        take(1),
        map(res => {
          return decodeUserPasswordSet(res);
        })
      );
  }
}
