import { execPipeline } from "../../../../utils/index.js";
import { unique } from "remeda";
const pivotIndicators = (chartPipeline)=>(spec, context)=>{
        const result = {
            ...spec
        };
        const { advancedVSeed } = context;
        const { measures, datasetReshapeInfo, dataset, encoding } = advancedVSeed;
        const colorItems = unique(datasetReshapeInfo.flatMap((d)=>d.unfoldInfo.colorItems));
        const allMeasureIds = unique(datasetReshapeInfo.flatMap((d)=>Object.keys(d.foldInfo.foldMap || {})));
        const indicators = datasetReshapeInfo.map((reshapeInfo, index)=>{
            const measureGroup = measures?.find((d)=>d.id === reshapeInfo.id);
            const subMeasuresId = (measureGroup?.children || []).map((d)=>d.id);
            const invalideMeasuresIds = allMeasureIds.filter((id)=>!subMeasuresId.includes(id));
            const newDataset = dataset[index];
            const newDatasetReshapeInfo = [
                {
                    ...reshapeInfo,
                    unfoldInfo: {
                        ...reshapeInfo.unfoldInfo,
                        colorItems
                    }
                }
            ];
            const newContext = {
                ...context,
                advancedVSeed: {
                    ...advancedVSeed,
                    datasetReshapeInfo: newDatasetReshapeInfo,
                    encoding: Object.keys(encoding).reduce((res, key)=>{
                        res[key] = encoding[key]?.filter((e)=>!invalideMeasuresIds.includes(e));
                        return res;
                    }, {}),
                    dataset: newDataset
                }
            };
            const chartSpec = execPipeline(chartPipeline, newContext, {});
            return {
                indicatorKey: reshapeInfo.id,
                title: measureGroup?.alias,
                cellType: 'chart',
                chartModule: 'vchart',
                chartSpec: chartSpec,
                style: {
                    padding: [
                        1,
                        1,
                        0,
                        1
                    ]
                }
            };
        });
        return {
            ...result,
            indicators: indicators
        };
    };
const pivotIndicatorsAsRow = (spec)=>{
    const result = {
        ...spec
    };
    return {
        ...result,
        indicatorsAsCol: false
    };
};
const pivotIndicatorsAsCol = (spec)=>{
    const result = {
        ...spec
    };
    return {
        ...result,
        indicatorsAsCol: true
    };
};
export { pivotIndicators, pivotIndicatorsAsCol, pivotIndicatorsAsRow };

//# sourceMappingURL=pivotIndicators.js.map