import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Constant } from '../helper/constant';
import { map } from 'rxjs';
import { EnvServiceFactory } from './env.service.provider';

@Injectable({
  providedIn: 'root'
})
export class ApiService {


  apiUrl: string = EnvServiceFactory().apiUrl;
  
  constructor(private httpClient : HttpClient) { }


  uploadFile(file: File){
    var formData = new FormData();
    formData.append("file", file);
    const headers = new HttpHeaders();
    return this.httpClient.post(this.apiUrl+"upload/pdf", formData, {headers:headers});
   }



  getDocuments(){
   return this.httpClient.get(this.apiUrl+"TDocument/get");
  }

  getFile(filename:string){
    const headers = new HttpHeaders().set("Accept", "application/pdf")
    return this.httpClient.get(this.apiUrl+"upload/documents/"+filename, {
      responseType : 'blob',
      headers : headers
    }).pipe(map((blobb: Blob) => {
      const file = new File([blobb], filename, {type : 'application/pdf'});
      return file;
    } ));
   }

}
