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

@Component({
    moduleId: module.id,
    selector: 'UnitStatsMembersCategory',
    templateUrl: './unit-stats-members-category.component.html',
    styleUrls: ['./unit-stats-members-category.component.scss']
})
export class UnitStatsMembersCategoryComponent {
    membersCategoryData: any;
    membersCategoryDataKeys: string[];

    @Input() set unitRegistrationMembersCategories(values: UnitRegistrationMembersCategoryOutput[]) {
        if (values) {
            this.membersCategoryData = 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.MembershipCategory)) {
                    acc[curr.MembershipCategory].members += addMembers;
                    acc[curr.MembershipCategory].fHMembers += addFHMembers;
                } else {
                    acc[curr.MembershipCategory] = { members: addMembers, fHMembers: addFHMembers };
                }

                return acc;
            }, {});

            this.membersCategoryDataKeys = Object.keys(this.membersCategoryData);
        } else {
            this.membersCategoryData = {};
            this.membersCategoryDataKeys = [];
        }
    }

}
