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

import {Observable} from 'rxjs';
import {catchError} from 'rxjs/operators';

import {EndpointConfigService} from 'm2-core';
import {CdCodeSnippet} from '../model/CdCodeSnippet';
import {CD_PLUGIN_CODE_EXAMPLES_CTX} from '../../../../config';
import {CdHttpClient} from './http/cd-http-client.service';


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

    constructor(
        private http: CdHttpClient,
        private endpointConfigService: EndpointConfigService
    ) {
    }

    public getCodeExample(section: string): Observable<CdCodeSnippet[]> {
        const url = this.endpointConfigService.baseUrl
            + `${CD_PLUGIN_CODE_EXAMPLES_CTX}/${section}.json`;
        return this.http.get<CdCodeSnippet[]>(url)
            .pipe(
                catchError(e => ([]))
            );
    }

}
