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

@Injectable({ providedIn: 'root' })
export class AppConfigService {
    private config: any;

    public loaded: boolean = false;

    constructor() { }

    loadConfig(): Observable<any> {
        return new Observable(observer => {
            fetch('/config.json')
                .then(async response => {
                    if (!response.ok) {
                        throw new Error(`Could not load config.json: ${response.statusText}`);
                    }
                    this.config = await response.json();
                    this.loaded = true;
                    observer.next(this.config);
                    observer.complete();
                })
                .catch(error => observer.error(error));
        });
    }

    get setting() {
        return this.config;
    }
}
