import { HttpErrorResponse } from '@angular/common/http';
import { Injectable} from '@angular/core';
import { HttpService } from './http.service';
import { Observable, throwError } from 'rxjs';
import { map, catchError } from 'rxjs/operators';

@Injectable()
export class LeadService {

  constructor(
    private httpService: HttpService,
  ) { }

  public getCreateLeadData(body, data?:any): Observable<any> {
    const getCustomerDataUrl = `https://vdzsts-api-dev.apac.bg.corpintra.net/sts-leads/api/v1/Lead/create`;
    return this.httpService.postOrPutRequest(getCustomerDataUrl, null, null, body, null, false, 'post', null, data)
      .pipe(
        map((responseData) => this.transformData(responseData)),
        catchError((error: any): any => {
          let abc = this.handleError(error);
          return abc;
        }));
  }

  public transformData(responseData) {
    console.log("rrrrrrrrrr", responseData);
    return responseData;
  }

  private handleError(err: HttpErrorResponse) {
    let errorMessage = '';
    if (err.error instanceof ErrorEvent) {
      // A client-side or network error occurred. Handle it accordingly.
      errorMessage = `An error occurred: ${err.error.message}`;
    } else {
      // The backend returned an unsuccessful response code.
      // The response body may contain clues as to what went wrong,
      errorMessage = `Server returned code: ${err.status}, error message is: ${err.message}`;
    }
    return throwError(errorMessage);
  }
}
