{"size": 1624, "ext": "js", "max_stars_repo_path": "web-page/src/components/FilterSideBar/EnergyFilter/index.js", "max_stars_repo_name": "hay-espacio-en-el-taco/dbs-scraper", "max_stars_repo_stars_event_min_datetime": "2018-05-02T17:48:55.000Z", "max_stars_repo_stars_event_max_datetime": "2021-07-29T02:13:32.000Z", "max_issues_repo_path": "web-page/src/components/FilterSideBar/EnergyFilter/index.js", "max_issues_repo_name": "hay-espacio-en-el-taco/dbs-scraper", "max_issues_repo_issues_event_min_datetime": "2018-05-02T17:49:45.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T10:33:35.000Z", "max_forks_repo_path": "web-page/src/components/FilterSideBar/EnergyFilter/index.js", "max_forks_repo_name": "hay-espacio-en-el-taco/dbs-scraper", "max_forks_repo_forks_event_min_datetime": "2018-05-02T15:57:07.000Z", "max_forks_repo_forks_event_max_datetime": "2020-02-17T05:07:08.000Z"}
{"max_stars_count": 5, "max_issues_count": 28, "max_forks_count": 9, "avg_line_length": 24.2388059701, "max_line_length": 65, "alphanum_fraction": 0.4827586207}
import React from 'react'
import FilterButtonsRow from '../FilterButtonsRow'
import { attributes as CardAttributes} from '../../../cards.json'


const ENERGY_VALUES = Array.from(
    CardAttributes.energy.reduce(
        (resultSet, item) => {
            const match = item.match(/^(?<number>\d*)/)
            if (!match) {
                return resultSet
            }
            
            const { groups: { number } } = match
            const energyCost = Number.parseInt(number, 10)
            resultSet.add(energyCost)

            return resultSet
        },
        new Set()
    ).values()
).sort(
    (a, b) => a -b
).map(
    i => ({ value: i, label: i })
)

const createFilter = (selectedValues = []) => {
    return card => {
        if (typeof card.energy !== 'string') {
            return false
        }
        
        const match = card.energy.match(/^(?<number>\d*)/)
        if (!match) {
            return false
        }
        
        const { groups: { number } } = match
        const energyCost = Number.parseInt(number, 10)
        if (Number.isNaN(energyCost)) {
            return false
        }
        
        return undefined !== selectedValues.find(
            item => {
                if (item === 6) {
                    return energyCost >= item 
                }

                return item === energyCost
            }
        )
    }
}

const CardTypeFilter = () => {
    return (
        <FilterButtonsRow
            title={'Energy Cost'}
            filterGenerator={createFilter}
            items={ENERGY_VALUES}
        />
    )
}

export default CardTypeFilter