import { Injectable } from '@angular/core';
import { DonutChartConfig, DossierState, IDossierState } from '@red/domain';
import { EntitiesMapService } from '@iqser/common-ui';
import { flatMap } from 'lodash-es';

@Injectable({ providedIn: 'root' })
export class DossierStatesMapService extends EntitiesMapService<IDossierState, DossierState> {
    get stats(): DonutChartConfig[] {
        const allStates = flatMap(Array.from(this._map.values()).map(obs => obs.value));
        return Array.from(
            allStates
                .reduce((acc, { color, name, dossierCount }) => {
                    const key = name + '-' + color;
                    const item: DonutChartConfig = acc.get(key) ?? Object.assign({}, { value: 0, label: name, color: color });
                    return acc.set(key, { ...item, value: item.value + dossierCount });
                }, new Map<string, DonutChartConfig>())
                .values(),
        );
    }
}
