import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {environment} from '../../../environments/environment';
import {map} from 'rxjs/operators';

@Injectable()
export class JournalService {

  constructor(private http: HttpClient) {}

  public getJournal(objTypeId: number = null, changeTypeId: number = null): Observable<any[]> {
    const qstring = environment.api + '/journal' +
      (objTypeId != null
        ? changeTypeId != null ? '/' + objTypeId + '/' + changeTypeId : '/' + objTypeId
        : '');

    return this.http.get(qstring)
      .pipe(map((response: any) => {
        return response as any[];
    }));
  }

  public getJournalWithDatePeriod(dateFrom: string, dateToInclusive: string, objTypeId: number = null, changeTypeId: number = null): Observable<any[]> {
    const qstring = environment.api + '/journal/' + dateFrom + '/' + dateToInclusive + '/' +
      (objTypeId != null
        ? changeTypeId != null ? '/' + objTypeId + '/' + changeTypeId : '/' + objTypeId
        : '');

    return this.http.get(qstring)
      .pipe(map((response: any) => {
        return response as any[];
      }));
  }
}
