import { Injectable, Output, EventEmitter } from '@angular/core';
import { HttpClient } from '@angular/common/http';
// import { Observable } from 'rxjs/Observable';
// import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable, BehaviorSubject } from 'rxjs';
import { map } from "rxjs/operators"; 
 // import 'rxjs/Rx';

@Injectable()
export class TreeService {
    constructor(private http: HttpClient) { }
    
   
    hostUrl=this.getHostURL();
    
        
    getJsonData ( fileName:any): Observable<any> 
    {
        console.log('file Name ',fileName);
        var SERVICE_URL = 'dashboard/assets/'+fileName+'.json';  // URL to web service
    //    var SERVICE_URL = 'assets/Assessment_Dashboard.json';  // URL to web service
        
        console.log('Inside get json data ');
        // return this.http.get<any>( SERVICE_URL)
        //                 .map( (res:any) => this.extractData(res) );
        return this.http.get<any>( SERVICE_URL)
                        .pipe(map( (res:any) => this.extractData(res) ));
    }
    
    private extractData(res: Response) {
        let body = res.json();
        console.log('extractData json[' + JSON.stringify(body) + ']');
        return body || { };
    }

    
    public getHostURL(): string {
        let HOST_URL: string = '';
        HOST_URL = localStorage.getItem( 'HOST_URL' );
        // HOST_URL = JSON.parse(localStorage.getItem( 'HOST_URL' )||'{}');
        if ( !HOST_URL ) HOST_URL = '';
        console.log( 'getHostURL[', HOST_URL, ']' );
        return HOST_URL;
    }
}
