import { Component, Input } from '@angular/core';
import { UnitRegistrationMembersAgeOutput } from '~/pages/unit/entities/output/unitRegistrationMembersAgeOutput';

@Component({
    moduleId: module.id,
    selector: 'UnitStatsMembersAge',
    templateUrl: './unit-stats-members-age.component.html',
    styleUrls: ['./unit-stats-members-age.component.scss']
})
export class UnitStatsMembersAgeComponent {
    membersAgeData: any;
    membersAgeDataKeys: string[];

    @Input() set unitRegistrationMembersAges(values: UnitRegistrationMembersAgeOutput[]) {
        if (values) {
            this.membersAgeData = values.reduce((acc: any, curr) => {
                let addMembers = 0;
                let addFHMembers = 0;

                if (['RadneCasopis', 'Radne', 'Cestne', 'HostovaniCizi'].indexOf(curr.Row) !== -1) {
                    addMembers = +curr.Count;
                }

                if (['RadneCasopis', 'Radne', 'Cestne'].indexOf(curr.Row) !== -1) {
                    addFHMembers = +curr.Count;
                }

                if (acc.hasOwnProperty(curr.AgeCategory)) {
                    acc[curr.AgeCategory].members += addMembers;
                    acc[curr.AgeCategory].fHMembers += addFHMembers;
                } else {
                    acc[curr.AgeCategory] = { members: addMembers, fHMembers: addFHMembers };
                }

                return acc;
            }, {});

            this.membersAgeDataKeys = Object.keys(this.membersAgeData);
        } else {
            this.membersAgeData = {};
            this.membersAgeDataKeys = [];
        }
    }

}
