
angular.module('DataFiltering', [])
.service('DataFiltering', [function() {
    var self = this;


    this.getColStats = (rows, colName, checkedList) => {

        const hash = rows.reduce((obj, row) => {
            if(row.hasOwnProperty(colName)){
                const item = row[colName];
                obj[item] = (obj.hasOwnProperty(item) ? obj[item] : 0) + 1;
            }
            return obj;
        }, {});

        delete hash['']

        // get list of objects
        const list = Object.entries(hash).map(entry => {
            return {
                name: entry[0],
                count: entry[1],
                checked: checkedList ? checkedList.includes(entry[0]) : false
            }
        })
        .sort((a, b) => b.count - a.count)

        return list;
    }

    this.getColStatsFromList = (rows, colName, checkedList) => {

        const hash = rows.reduce((obj, row) => {
            if(row.hasOwnProperty(colName)){
                row[colName].forEach(item => {
                    obj[item] = (obj.hasOwnProperty(item) ? obj[item] : 0) + 1;
                });
            }
            return obj;
        }, {})

        // get list of objects
        const list = Object.entries(hash).map(entry => {
            return {
                name: entry[0],
                count: entry[1],
                checked: checkedList ? checkedList.includes(entry[0]) : false
            }
        })
        .sort((a, b) => b.count - a.count)

        return list;
    }

}])
