import { Injectable } from '@angular/core';
import { DonutChartConfig } from '@red/domain';
import { TranslateService } from '@ngx-translate/core';
import { rolesTranslations } from '@translations/roles-translations';
import { workflowFileStatusTranslations } from '@translations/file-status-translations';
import { DossierStatesMapService } from './entity-services/dossier-states-map.service';

@Injectable({
    providedIn: 'root',
})
export class TranslateChartService {
    constructor(private readonly _translateService: TranslateService, private readonly _dossierStatesMapService: DossierStatesMapService) {}

    translateLabels(config: DonutChartConfig[]): DonutChartConfig[] {
        return config.map(val => ({ ...val, label: this._translateService.instant(val.label) }));
    }

    translateDossierStates(config: DonutChartConfig[], dossierTemplateId: string): DonutChartConfig[] {
        return config.map(val => {
            if (val.key === 'undefined') {
                return { ...val, label: this._translateService.instant(val.label) };
            }
            const dossierState = this._dossierStatesMapService.get(dossierTemplateId, val.key);
            return { ...val, label: dossierState.name, color: dossierState.color };
        });
    }

    translateWorkflowStatus(config: DonutChartConfig[]): DonutChartConfig[] {
        return config.map(val => ({ ...val, label: this._translateService.instant(workflowFileStatusTranslations[val.label] as string) }));
    }

    translateRoles(config: DonutChartConfig[]): DonutChartConfig[] {
        return config.map(val => ({
            ...val,
            label: this._translateService.instant(rolesTranslations[val.label]).toLowerCase(),
        }));
    }
}
