import {isEqual} from 'lodash';

export const addToCategories = (categories) => addTo('categories', categories.map(({id, name}) => ({id, name})))
export const addToTags = (tags) => addTo('tags', tags.map(({id, name}) => ({id, name})))

export const mapNameAndIdToOptions = (items) => mapToOptions(items, {value: 'id', label: 'name'})

export const mapToOptions = (items, keys) => items.map(item => ({
    value: item[keys.value],
    label: item[keys.label]
}))

const addTo = (property, items) => {
    const existingItems = CouponsPlus[property]

    for (let item of items) {
        const itemExists = existingItems.find(existingItem => isEqual(existingItem, item))
        if (!itemExists) {
            existingItems.push(item)
        }
    }
}