import { FoldMeasureId, FoldMeasureName, LowerWhisker, MedianMeasureId, OutliersMeasureId, Q1MeasureValue, Q3MeasureValue, Separator, UpperWhisker, unfoldDimensions } from "../../../../../dataReshape/index.js";
import { boxplot } from "@visactor/vdataset";
import { uniqueBy } from "remeda";
const reshapeWithBoxplotEncoding = (advancedVSeed, context)=>{
    const result = {
        ...advancedVSeed
    };
    const { vseed } = context;
    const { dataset, chartType } = vseed;
    const { encoding = {}, config, measures = [] } = advancedVSeed;
    const dimensions = advancedVSeed.reshapeDimensions ?? advancedVSeed.dimensions ?? [];
    const uniqDims = uniqueBy(dimensions, (item)=>item.id);
    const whiskers = config?.[chartType]?.whiskers;
    let newDatasets = [];
    let foldInfo = {};
    let unfoldInfo = {};
    if (encoding.value?.length) {
        const boxPlotDataList = [];
        encoding.value.forEach((f)=>{
            const m = measures.find((m)=>m.id === f);
            const boxPlotData = boxplot(dataset, {
                field: f,
                groupField: [
                    ...encoding.x ?? [],
                    ...encoding.color ?? []
                ],
                whiskers,
                outputNames: {
                    q1: Q1MeasureValue,
                    q3: Q3MeasureValue,
                    lowerWhisker: LowerWhisker,
                    upperWhisker: UpperWhisker,
                    median: MedianMeasureId,
                    outliers: OutliersMeasureId
                }
            });
            boxPlotData.forEach((datum)=>{
                datum[FoldMeasureId] = f;
                datum[FoldMeasureName] = m?.alias ?? f;
            });
            boxPlotDataList.push(...boxPlotData);
        });
        const res = unfoldDimensions(boxPlotDataList, uniqDims, encoding, {
            foldMeasureId: FoldMeasureId,
            separator: Separator,
            colorItemAsId: false
        });
        res.dataset.forEach((d)=>{
            newDatasets.push(d);
        });
        unfoldInfo = res.unfoldInfo;
    } else if (encoding.q1?.length && encoding.q3?.length && encoding.min?.length && encoding.max?.length && encoding.median?.length) {
        const res = unfoldDimensions(dataset, uniqDims, encoding, {
            foldMeasureId: FoldMeasureId,
            separator: Separator,
            colorItemAsId: false
        });
        res.dataset.forEach((datum)=>{
            datum[UpperWhisker] = datum[encoding.max[0]];
            datum[LowerWhisker] = datum[encoding.min[0]];
            datum[Q1MeasureValue] = datum[encoding.q1[0]];
            datum[Q3MeasureValue] = datum[encoding.q3[0]];
            datum[MedianMeasureId] = datum[encoding.median[0]];
        });
        newDatasets = res.dataset;
        foldInfo = {};
        unfoldInfo = res.unfoldInfo;
    }
    return {
        ...result,
        dataset: newDatasets,
        datasetReshapeInfo: [
            {
                id: String(chartType),
                index: 0,
                foldInfo,
                unfoldInfo
            }
        ]
    };
};
export { reshapeWithBoxplotEncoding };

//# sourceMappingURL=reshapeWithBoxplotEncoding.js.map