'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var types = require('../dashboard-visualisation/types.js');

const mergeScorecardsIntoGroup = (visualisations, isEnabled) => {
    const groupedScorecardIndexes = visualisations
        // get scorecard indexes
        .reduce((acc, vis, i) => {
        if (vis.type === types.DashboardVisualisationType.SCORECARD)
            acc.push(i);
        return acc;
    }, [])
        // group adjacent indexes
        .reduce((r, n) => {
        const lastSubArray = r[r.length - 1];
        if (!lastSubArray || lastSubArray[lastSubArray.length - 1] !== n - 1)
            r.push([]);
        r[r.length - 1].push(n);
        return r;
    }, []);
    groupedScorecardIndexes.reverse().forEach((group) => {
        const spliceAtIndex = group[0];
        const scorecardGroup = group
            .map((scIndex) => {
            return visualisations[scIndex].data;
        })
            .filter((scorecard) => !!scorecard);
        while (group.length) {
            const i = group.pop();
            if (i !== undefined)
                visualisations.splice(i, 1);
        }
        if (scorecardGroup.length) {
            visualisations.splice(spliceAtIndex, 0, {
                id: `${spliceAtIndex}`,
                type: types.DashboardVisualisationType.SCORECARD_GROUP,
                data: [{ scorecards: scorecardGroup }],
                isEnabled,
            });
        }
    });
    return visualisations;
};
var ScorecardsUtils = {
    mergeScorecardsIntoGroup,
};

exports.default = ScorecardsUtils;
exports.mergeScorecardsIntoGroup = mergeScorecardsIntoGroup;
//# sourceMappingURL=utils.js.map
