/**
 * DevExtreme (esm/core/utils/array.js)
 * Version: 22.1.3 (build 22180-0310)
 * Build date: Wed Jun 29 2022
 *
 * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import {
    isDefined
} from "./type";
import {
    orderEach
} from "./object";
import config from "../config";
export var wrapToArray = function(item) {
    return Array.isArray(item) ? item : [item]
};
export var getUniqueValues = function(values) {
    return [...new Set(values)]
};
export var removeDuplicates = function() {
    var from = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
    var toRemove = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : [];
    var toRemoveMap = toRemove.reduce((map, value) => {
        var _map$value;
        map[value] = (null !== (_map$value = map[value]) && void 0 !== _map$value ? _map$value : 0) + 1;
        return map
    }, {});
    return from.filter(value => !toRemoveMap[value]--)
};
export var normalizeIndexes = function(items, indexPropName, currentItem, needIndexCallback) {
    var indexedItems = {};
    var {
        useLegacyVisibleIndex: useLegacyVisibleIndex
    } = config();
    var currentIndex = 0;
    var shouldUpdateIndex = item => !isDefined(item[indexPropName]) && (!needIndexCallback || needIndexCallback(item));
    items.forEach(item => {
        var index = item[indexPropName];
        if (index >= 0) {
            indexedItems[index] = indexedItems[index] || [];
            if (item === currentItem) {
                indexedItems[index].unshift(item)
            } else {
                indexedItems[index].push(item)
            }
        } else {
            item[indexPropName] = void 0
        }
    });
    if (!useLegacyVisibleIndex) {
        items.forEach(item => {
            if (shouldUpdateIndex(item)) {
                while (indexedItems[currentIndex]) {
                    currentIndex++
                }
                indexedItems[currentIndex] = [item];
                currentIndex++
            }
        })
    }
    currentIndex = 0;
    orderEach(indexedItems, (function(index, items) {
        items.forEach(item => {
            if (index >= 0) {
                item[indexPropName] = currentIndex++
            }
        })
    }));
    if (useLegacyVisibleIndex) {
        items.forEach(item => {
            if (shouldUpdateIndex(item)) {
                item[indexPropName] = currentIndex++
            }
        })
    }
};
export var groupBy = (array, getGroupName) => array.reduce((groupedResult, item) => {
    var _groupedResult$groupN;
    var groupName = getGroupName(item);
    groupedResult[groupName] = null !== (_groupedResult$groupN = groupedResult[groupName]) && void 0 !== _groupedResult$groupN ? _groupedResult$groupN : [];
    groupedResult[groupName].push(item);
    return groupedResult
}, {});
