import { uniqueBy } from "remeda";
import { FoldMeasureValue, dataReshapeByEncoding } from "../../../../../dataReshape/index.js";
import { getColorMeasureId } from "../../../../spec/chart/pipes/index.js";
import { findAllMeasures } from "../../../../utils/index.js";
const pivotReshapeWithEncoding = (advancedVSeed, context)=>{
    const result = {
        ...advancedVSeed
    };
    const { vseed } = context;
    const { dataset } = vseed;
    const { encoding } = advancedVSeed;
    const measures = advancedVSeed.reshapeMeasures ?? advancedVSeed.measures ?? [];
    const dimensions = advancedVSeed.reshapeDimensions ?? advancedVSeed.dimensions ?? [];
    const allMeasures = findAllMeasures(measures);
    const measureGroups = [];
    if (measures) measures.forEach((measure)=>{
        if (measure.children && measure.children.length > 0) measureGroups.push(measure);
    });
    const datasets = [];
    const datasetReshapeInfo = [];
    measureGroups.forEach((measureGroup, index)=>{
        const measures = measureGroup.children;
        if (!measures) return;
        const groupId = measureGroup.id;
        const { dataset: newSubDataset, foldInfo, unfoldInfo } = dataReshapeByEncoding(dataset, uniqueBy(dimensions, (item)=>item.id), uniqueBy(measures, (item)=>item.id), encoding, {
            colorItemAsId: false,
            foldMeasureValue: `${FoldMeasureValue}${groupId}`,
            colorMeasureId: getColorMeasureId(advancedVSeed, vseed),
            omitIds: allMeasures.map((item)=>item.id)
        });
        const reshapeInfo = {
            id: groupId,
            index,
            foldInfo,
            unfoldInfo
        };
        datasets.push(newSubDataset);
        datasetReshapeInfo.push(reshapeInfo);
    });
    return {
        ...result,
        dataset: datasets,
        datasetReshapeInfo: datasetReshapeInfo
    };
};
export { pivotReshapeWithEncoding };

//# sourceMappingURL=pivotReshapeWithEncoding.js.map