import { execPipeline, isRectungularCoordinate } from "../../../../utils/index.js";
import { unique } from "remeda";
const pivotIndicators = (chartPipeline)=>(spec, context)=>{
        const result = {
            ...spec
        };
        const { advancedVSeed } = context;
        const { datasetReshapeInfo, dataset, encoding, reshapeMeasures = [] } = 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 subMeasures = reshapeMeasures[index] || [];
            const subMeasuresId = (reshapeMeasures[index] || []).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,
                    pivotAllDatasetReshapeInfo: datasetReshapeInfo,
                    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: isRectungularCoordinate(advancedVSeed.chartType) ? '' : subMeasures.map((m)=>m.alias ?? m.id).join('-'),
                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