import { isMeasureTreeWithChildren, isMeasureTreeWithParentId, normalizeMeasureTree } from "./utils.js";
import { isPivotChart } from "../../../../utils/index.js";
import { DEFAULT_PARENT_ID } from "../../../../utils/constant.js";
const buildMeasures = (advancedVSeed)=>{
    if (isMeasureTreeWithChildren(advancedVSeed.measures)) {
        advancedVSeed.measures = normalizeMeasureTree(advancedVSeed.measures);
        return advancedVSeed;
    }
    if (isMeasureTreeWithParentId(advancedVSeed.measures)) {
        advancedVSeed.measures = generateMeasuresByParentId(advancedVSeed.measures);
        return advancedVSeed;
    }
    if (isPivotChart(advancedVSeed)) advancedVSeed.measures = basicMeasuresToMeasureTree(advancedVSeed.measures);
    return advancedVSeed;
};
const generateMeasuresByParentId = (measures)=>{
    const measureTree = [];
    measures.forEach((measure)=>{
        const parent = measureTree.find((item)=>item.id === measure.parentId);
        if (parent && 'children' in parent) {
            parent.children = parent.children || [];
            parent.children.push(measure);
        } else if (measure.parentId) measureTree.push({
            id: measure.parentId,
            children: [
                measure
            ]
        });
        else measureTree.push({
            id: DEFAULT_PARENT_ID,
            children: [
                measure
            ]
        });
    });
    return measureTree;
};
const basicMeasuresToMeasureTree = (measures)=>{
    const id = measures.map((item)=>item.id).join('-');
    const alias = measures.map((item)=>item.alias || item.id).join('-');
    return [
        {
            id,
            alias,
            children: measures
        }
    ];
};
export { buildMeasures };

//# sourceMappingURL=buildMeasures.js.map