/**
 * DevExtreme (esm/__internal/grids/grid_core/data_source_adapter/m_data_source_adapter_utils.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 _extends from "@babel/runtime/helpers/esm/extends";
import {
    executeAsync
} from "../../../../core/utils/common";
import {
    extend
} from "../../../../core/utils/extend";
import {
    isDefined
} from "../../../../core/utils/type";
import gridCoreUtils from "../m_utils";
export var cloneItems = function cloneItems(items, groupCount) {
    if (items) {
        items = items.slice(0);
        if (groupCount) {
            for (var i = 0; i < items.length; i++) {
                items[i] = extend({
                    key: items[i].key
                }, items[i]);
                items[i].items = cloneItems(items[i].items, groupCount - 1)
            }
        }
    }
    return items
};
export var calculateOperationTypes = function(loadOptions, lastLoadOptions, isFullReload) {
    var operationTypes = {
        reload: true,
        fullReload: true
    };
    if (lastLoadOptions) {
        operationTypes = {
            sorting: !gridCoreUtils.equalSortParameters(loadOptions.sort, lastLoadOptions.sort),
            grouping: !gridCoreUtils.equalSortParameters(loadOptions.group, lastLoadOptions.group, true),
            groupExpanding: !gridCoreUtils.equalSortParameters(loadOptions.group, lastLoadOptions.group) || lastLoadOptions.groupExpand,
            filtering: !gridCoreUtils.equalFilterParameters(loadOptions.filter, lastLoadOptions.filter),
            pageIndex: loadOptions.pageIndex !== lastLoadOptions.pageIndex,
            skip: loadOptions.skip !== lastLoadOptions.skip,
            take: loadOptions.take !== lastLoadOptions.take,
            pageSize: loadOptions.pageSize !== lastLoadOptions.pageSize,
            fullReload: isFullReload,
            reload: false,
            paging: false
        };
        operationTypes.reload = isFullReload || operationTypes.sorting || operationTypes.grouping || operationTypes.filtering;
        operationTypes.paging = operationTypes.pageIndex || operationTypes.pageSize || operationTypes.take
    }
    return operationTypes
};
export var executeTask = function(action, timeout) {
    if (isDefined(timeout)) {
        executeAsync(action, timeout)
    } else {
        action()
    }
};
export var createEmptyCachedData = function() {
    return {
        items: {}
    }
};
export var getPageDataFromCache = function(options, updatePaging) {
    var groupCount = gridCoreUtils.normalizeSortingInfo(options.group || options.storeLoadOptions.group || options.loadOptions.group).length;
    var items = [];
    if (fillItemsFromCache(items, options, groupCount)) {
        return items
    }
    if (updatePaging) {
        updatePagingOptionsByCache(items, options, groupCount)
    }
};
export var fillItemsFromCache = function(items, options, groupCount, fromEnd) {
    var _a, _b, _c, _d, _e;
    var {
        storeLoadOptions: storeLoadOptions
    } = options;
    var take = null !== (_b = null !== (_a = options.take) && void 0 !== _a ? _a : storeLoadOptions.take) && void 0 !== _b ? _b : 0;
    var cachedItems = null === (_c = options.cachedData) || void 0 === _c ? void 0 : _c.items;
    if (take && cachedItems) {
        var skip = null !== (_e = null !== (_d = options.skip) && void 0 !== _d ? _d : storeLoadOptions.skip) && void 0 !== _e ? _e : 0;
        for (var i = 0; i < take; i += 1) {
            var localIndex = fromEnd ? take - 1 - i : i;
            var cacheItemIndex = localIndex + skip;
            var cacheItem = cachedItems[cacheItemIndex];
            if (void 0 === cacheItem && cacheItemIndex in cachedItems) {
                return true
            }
            var item = getItemFromCache(options, cacheItem, groupCount, localIndex, take);
            if (item) {
                items.push(item)
            } else {
                return false
            }
        }
        return true
    }
    return false
};
export var getItemFromCache = function(options, cacheItem, groupCount, index, take) {
    if (groupCount && cacheItem) {
        var skips = 0 === index && options.skips || [];
        var takes = index === take - 1 && options.takes || [];
        return getGroupItemFromCache(cacheItem, groupCount, skips, takes)
    }
    return cacheItem
};
export var getGroupItemFromCache = function getGroupItemFromCache(cacheItem, groupCount, skips, takes) {
    if (groupCount && cacheItem) {
        var result = _extends({}, cacheItem);
        var skip = skips[0] || 0;
        var take = takes[0];
        var {
            items: items
        } = cacheItem;
        if (items) {
            if (void 0 === take && !items[skip]) {
                return
            }
            result.items = [];
            if (skips.length) {
                result.isContinuation = true
            }
            if (take) {
                result.isContinuationOnNextPage = cacheItem.count > take
            }
            for (var i = 0; void 0 === take ? items[i + skip] : i < take; i += 1) {
                var childCacheItem = items[i + skip];
                var isLast = i + 1 === take;
                var item = getGroupItemFromCache(childCacheItem, groupCount - 1, 0 === i ? skips.slice(1) : [], isLast ? takes.slice(1) : []);
                if (void 0 !== item) {
                    result.items.push(item)
                } else {
                    return
                }
            }
        }
        return result
    }
    return cacheItem
};
export var updatePagingOptionsByCache = function(cacheItemsFromBegin, options, groupCount) {
    var _a, _b;
    var cacheItemBeginCount = cacheItemsFromBegin.length;
    var {
        storeLoadOptions: storeLoadOptions
    } = options;
    if (void 0 !== storeLoadOptions.skip && storeLoadOptions.take && !groupCount) {
        var cacheItemsFromEnd = [];
        fillItemsFromCache(cacheItemsFromEnd, options, groupCount, true);
        var cacheItemEndCount = cacheItemsFromEnd.length;
        if (cacheItemBeginCount || cacheItemEndCount) {
            options.skip = null !== (_a = options.skip) && void 0 !== _a ? _a : storeLoadOptions.skip;
            options.take = null !== (_b = options.take) && void 0 !== _b ? _b : storeLoadOptions.take
        }
        if (cacheItemBeginCount) {
            storeLoadOptions.skip += cacheItemBeginCount;
            storeLoadOptions.take -= cacheItemBeginCount;
            options.cachedDataPartBegin = cacheItemsFromBegin
        }
        if (cacheItemEndCount) {
            storeLoadOptions.take -= cacheItemEndCount;
            options.cachedDataPartEnd = cacheItemsFromEnd.reverse()
        }
    }
};
export var setPageDataToCache = function(options, data, groupCount) {
    var _a, _b, _c, _d;
    var {
        storeLoadOptions: storeLoadOptions
    } = options;
    var skip = null !== (_b = null !== (_a = options.skip) && void 0 !== _a ? _a : storeLoadOptions.skip) && void 0 !== _b ? _b : 0;
    var take = null !== (_d = null !== (_c = options.take) && void 0 !== _c ? _c : storeLoadOptions.take) && void 0 !== _d ? _d : 0;
    for (var i = 0; i < take; i += 1) {
        var globalIndex = i + skip;
        var cacheItems = options.cachedData.items;
        var skips = 0 === i && options.skips || [];
        cacheItems[globalIndex] = getCacheItem(cacheItems[globalIndex], data[i], groupCount, skips)
    }
};
export var getCacheItem = function getCacheItem(cacheItem, loadedItem, groupCount, skips) {
    if (groupCount && loadedItem) {
        var result = _extends({}, loadedItem);
        delete result.isContinuation;
        delete result.isContinuationOnNextPage;
        var skip = skips[0] || 0;
        if (loadedItem.items) {
            result.items = (null === cacheItem || void 0 === cacheItem ? void 0 : cacheItem.items) || {};
            loadedItem.items.forEach((item, index) => {
                var globalIndex = index + skip;
                var childSkips = 0 === index ? skips.slice(1) : [];
                result.items[globalIndex] = getCacheItem(result.items[globalIndex], item, groupCount - 1, childSkips)
            })
        }
        return result
    }
    return loadedItem
};
