// Questo file fa parte di MyTown.
// MyTown è software libero: puoi ridistribuirlo e/o modificarlo secondo
// i termini della GNU Affero General Public License come pubblicata dalla
// Free Software Foundation, versione 3.
// Distribuito senza ALCUNA GARANZIA; vedi la GNU AGPL v3 per i dettagli.
// Copyright (C) Kondor Consulting & Services S.r.l.
// SPDX-License-Identifier: AGPL-3.0-or-later

import { TranslateLoader } from '@ngx-translate/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { Injector } from '@angular/core';
import { Router } from '@angular/router';
import { environment } from '../../environments/environment';

export class CustomTranslateLoader implements TranslateLoader {
	private router: Router;

	constructor(
		private httpClient: HttpClient,
		private injector: Injector,
	) {
		this.router = this.injector.get(Router);
	}
	getTranslation(lang: string): Observable<any> {
		return new Observable((observer) => {
			// URL traduzioni da environment (build-time), non più da config.json runtime.
			const url = environment.base_url + environment.service_url_login + 'translate/' + lang;
			this.httpClient.get(url).subscribe({
				next: (translationData) => {
					observer.next(translationData);
					observer.complete();
				},
				error: (error) => {
					observer.error(error);
					this.router.navigate(['503']);
				},
			});
		});
	}
}
