import { DictionarySummary, IDossierTemplateStats } from './dossier-template-stats';

export class DossierTemplateStats implements IDossierTemplateStats {
    readonly dossierTemplateId: string;
    readonly dictionarySummaryList: DictionarySummary[];
    readonly numberOfDictionaries: number;

    readonly numberOfEntries: number;
    private readonly _dictionarySummaryMap = new Map<string, DictionarySummary>();

    constructor(stats: IDossierTemplateStats) {
        this.dossierTemplateId = stats.dossierTemplateId;
        this.dictionarySummaryList = stats.dictionarySummaryList;
        this.numberOfDictionaries = stats.numberOfDictionaries;

        this.numberOfEntries = this.dictionarySummaryList.reduce((counter, summary) => counter + summary.entriesCount, 0);
        this.dictionarySummaryList.forEach(summary => this._dictionarySummaryMap.set(summary.type, summary));
    }

    dictionarySummary(type: string): DictionarySummary | undefined {
        const defaultValue: DictionarySummary = { entriesCount: 0, id: type, name: type, type };
        if (!this._dictionarySummaryMap.get(type)) {
            // TODO
            console.error(`Missing type ${type}`);
        }
        return this._dictionarySummaryMap.get(type) || defaultValue;
    }
}
