import { unique, uniqueBy } from "remeda";
import { DimAxisType, FoldPrimaryMeasureValue, FoldSecondaryMeasureValue, dataReshapeByEncoding } from "../../../../../dataReshape/index.js";
import { getColorMeasureId } from "../../../../spec/chart/pipes/index.js";
import { DEFAULT_DUAL_CHART_TYPE } from "../../../../utils/chatType.js";
const reshapeWithDualEncoding = (advancedVSeed, context)=>{
    const result = {
        ...advancedVSeed
    };
    const { vseed } = context;
    const { dataset } = vseed;
    const { encoding, chartType } = advancedVSeed;
    const measures = advancedVSeed.reshapeMeasures?.[0] ?? [];
    const dimensions = advancedVSeed.reshapeDimensions ?? advancedVSeed.dimensions ?? [];
    const allMeasuresIds = measures.map((m)=>m.id);
    const foldInfoList = [];
    const unfoldInfoList = [];
    const datasets = [];
    const primaryMeasures = measures.filter((m)=>'primaryYAxis' === m.encoding);
    const secondaryMeasures = measures.filter((m)=>'secondaryYAxis' === m.encoding);
    const primaryChartTypes = unique(primaryMeasures.map((m)=>m.chartType));
    const secondaryChartTypes = unique(secondaryMeasures.map((m)=>m.chartType));
    if (!primaryChartTypes.length) primaryChartTypes.push(DEFAULT_DUAL_CHART_TYPE.primary);
    if (!secondaryChartTypes.length) secondaryChartTypes.push(DEFAULT_DUAL_CHART_TYPE.secondary);
    primaryChartTypes.forEach((chartType)=>{
        const primaryResult = dataReshapeByEncoding(dataset, uniqueBy(dimensions, (item)=>item.id), uniqueBy(primaryMeasures.filter((m)=>m.chartType === chartType), (item)=>item.id), encoding, {
            colorItemAsId: false,
            foldMeasureValue: FoldPrimaryMeasureValue,
            colorMeasureId: getColorMeasureId(advancedVSeed, vseed),
            omitIds: allMeasuresIds
        });
        primaryResult.dataset.forEach((row)=>{
            row[DimAxisType] = 'primaryYAxis';
        });
        datasets.push(primaryResult.dataset);
        foldInfoList.push(primaryResult.foldInfo);
        unfoldInfoList.push(primaryResult.unfoldInfo);
    });
    secondaryChartTypes.forEach((chartType)=>{
        const secondaryResult = dataReshapeByEncoding(dataset, uniqueBy(dimensions, (item)=>item.id), uniqueBy(secondaryMeasures.filter((m)=>m.chartType === chartType), (item)=>item.id), encoding, {
            colorItemAsId: false,
            foldMeasureValue: FoldSecondaryMeasureValue,
            colorMeasureId: getColorMeasureId(advancedVSeed, vseed),
            omitIds: allMeasuresIds
        });
        secondaryResult.dataset.forEach((row)=>{
            row[DimAxisType] = 'secondaryYAxis';
        });
        datasets.push(secondaryResult.dataset);
        foldInfoList.push(secondaryResult.foldInfo);
        unfoldInfoList.push(secondaryResult.unfoldInfo);
    });
    const unfoldInfo = {
        ...unfoldInfoList[0],
        colorItems: unfoldInfoList.flatMap((d)=>d.colorItems),
        colorIdMap: unfoldInfoList.reduce((prev, cur)=>({
                ...prev,
                ...cur.colorIdMap
            }), {})
    };
    return {
        ...result,
        dataset: datasets,
        datasetReshapeInfo: [
            {
                id: String(chartType),
                index: 0,
                foldInfo: foldInfoList[0],
                foldInfoList: foldInfoList,
                unfoldInfo: unfoldInfo
            }
        ]
    };
};
export { reshapeWithDualEncoding };

//# sourceMappingURL=reshapeWithDualEncoding.js.map