import { unique } from "remeda";
import { FoldXMeasureId, FoldXMeasureValue, FoldYMeasureId, FoldYMeasureValue, ORIGINAL_DATA, dataReshapeByEncoding } from "../../../../../dataReshape/index.js";
import { getColorMeasureId } from "../../../../spec/chart/pipes/index.js";
const pivotReshapeWithScatterEncoding = (advancedVSeed, context)=>{
    const result = {
        ...advancedVSeed
    };
    const { vseed } = context;
    const { dataset } = vseed;
    const { encoding, chartType } = advancedVSeed;
    const reshapeMeasures = advancedVSeed.reshapeMeasures ?? [];
    const dimensions = advancedVSeed.reshapeDimensions ?? advancedVSeed.dimensions ?? [];
    let allMeasuresIds = unique(reshapeMeasures.flatMap((measureGroup)=>measureGroup.map((m)=>m.id)));
    if (encoding?.size?.length) allMeasuresIds = allMeasuresIds.filter((mId)=>!encoding.size.includes(mId));
    const datasetList = [];
    const datasetReshapeInfo = [];
    reshapeMeasures.forEach((measures, index)=>{
        const foldInfoList = [];
        const unfoldInfoList = [];
        const datasets = [];
        const xMeasures = measures.filter((m)=>'xAxis' === m.encoding);
        const yMeasures = measures.filter((m)=>'yAxis' === m.encoding);
        const xResult = dataReshapeByEncoding(dataset, dimensions, xMeasures, encoding, {
            foldMeasureValue: `${FoldXMeasureValue}${index}`,
            foldMeasureId: FoldXMeasureId,
            colorItemAsId: true,
            colorMeasureId: getColorMeasureId(advancedVSeed, vseed),
            omitIds: allMeasuresIds,
            locale: advancedVSeed.locale
        });
        datasets.push(xResult.dataset);
        foldInfoList.push(xResult.foldInfo);
        unfoldInfoList.push(xResult.unfoldInfo);
        const yResult = dataReshapeByEncoding(dataset, dimensions, yMeasures, encoding, {
            foldMeasureValue: `${FoldYMeasureValue}${index}`,
            foldMeasureId: FoldYMeasureId,
            colorItemAsId: true,
            colorMeasureId: getColorMeasureId(advancedVSeed, vseed),
            omitIds: allMeasuresIds,
            locale: advancedVSeed.locale
        });
        datasets.push(yResult.dataset);
        foldInfoList.push(yResult.foldInfo);
        unfoldInfoList.push(yResult.unfoldInfo);
        const unfoldInfo = {
            ...unfoldInfoList[0],
            colorItems: unfoldInfoList.flatMap((d)=>d.colorItems),
            colorIdMap: unfoldInfoList.reduce((prev, cur)=>({
                    ...prev,
                    ...cur.colorIdMap
                }), {})
        };
        const reshapeInfo = {
            id: `${chartType}-${index}`,
            index,
            foldInfo: foldInfoList[0],
            foldInfoList: foldInfoList,
            unfoldInfo: unfoldInfo
        };
        datasetReshapeInfo.push(reshapeInfo);
        datasetList.push(datasets[0].flatMap((d)=>datasets[1].filter((yDatum)=>yDatum[ORIGINAL_DATA] === d[ORIGINAL_DATA]).map((yDatum)=>({
                    ...d,
                    ...yDatum
                }))));
    });
    return {
        ...result,
        dataset: datasetList,
        datasetReshapeInfo: datasetReshapeInfo
    };
};
export { pivotReshapeWithScatterEncoding };

//# sourceMappingURL=pivotReshapeWithScatterEncoding.js.map