/**
 * DevExtreme (esm/core/utils/array.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 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";

function createOccurrenceMap(array) {
    return array.reduce((map, value) => {
        var _map$get;
        var count = (null !== (_map$get = map.get(value)) && void 0 !== _map$get ? _map$get : 0) + 1;
        map.set(value, count);
        return map
    }, new Map)
}
export var wrapToArray = function(item) {
    return Array.isArray(item) ? item : [item]
};
export var getUniqueValues = function(values) {
    return [...new Set(values)]
};
export var getIntersection = function(firstArray, secondArray) {
    var toRemoveMap = createOccurrenceMap(secondArray);
    return firstArray.filter(value => {
        var occurrencesCount = toRemoveMap.get(value);
        occurrencesCount && toRemoveMap.set(value, occurrencesCount - 1);
        return occurrencesCount
    })
};
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 = createOccurrenceMap(toRemove);
    return from.filter(value => {
        var occurrencesCount = toRemoveMap.get(value);
        occurrencesCount && toRemoveMap.set(value, occurrencesCount - 1);
        return !occurrencesCount
    })
};
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
}, {});
