// (C) 2019-2026 GoodData Corporation
import { merge, uniqBy } from "lodash-es";
const DefaultPageSize = 250;
/**
 * Tiger organization utility functions
 *
 * @internal
 */
export class OrganizationUtilities {
    /**
     * Guard for recognizing entities which support `included` field.
     * @internal
     */
    static supportsIncluded(entity) {
        return entity.included !== undefined;
    }
    static getAllPagesOf = async (client, entitiesGet, params, options = {}) => {
        const results = [];
        const pageSize = params?.size ?? DefaultPageSize;
        let reachedEnd = false;
        let nextPage = 0;
        while (!reachedEnd) {
            const result = await entitiesGet(client.axios, "", { ...params, page: nextPage, size: pageSize }, merge({}, options));
            results.push(result.data);
            if (result.data.data.length < pageSize) {
                reachedEnd = true;
            }
            else {
                nextPage += 1;
            }
        }
        return results;
    };
    /**
     * This function merges multiple pages containing metadata entities into a single page. The entity data from different
     * pages are concatenated. The side-loaded entities are concatenated and their uniqueness is ensured so that same
     * entity sideloaded on multiple pages only appears once.
     *
     * The merges result WILL NOT contain any links section.
     *
     * @param pages - pages to merge
     * @internal
     */
    static mergeEntitiesResults(pages) {
        return {
            data: pages.flatMap((page) => page.data),
            included: uniqBy(pages.flatMap((page) => OrganizationUtilities.supportsIncluded(page) ? (page.included ?? []) : []), (item) => `${item.id}_${item.type}`),
        };
    }
}
//# sourceMappingURL=organizationUtilities.js.map