import { unique } from "remeda";
import { isPivotTable, isTable } from "../../../pipeline/index.js";
import { isLinearColor } from "../../../pipeline/spec/chart/pipes/index.js";
import { createSpecifiedForColorMapping } from "../../../pipeline/spec/chart/pipes/color/color.js";
const getColorItems = (builder)=>{
    const advancedVSeed = builder.advancedVSeed;
    if (!advancedVSeed || isTable(builder.vseed) || isPivotTable(builder.vseed)) return [];
    const { datasetReshapeInfo } = advancedVSeed;
    const colorItems = unique(datasetReshapeInfo.flatMap((d)=>d.unfoldInfo.colorItems));
    const colorIdMap = datasetReshapeInfo.reduce((prev, cur)=>({
            ...prev,
            ...cur.unfoldInfo.colorIdMap
        }), {});
    return colorItems.map((d)=>({
            id: d,
            alias: colorIdMap[d]?.alias
        }));
};
const getColorIdMap = (builder)=>{
    const advancedVSeed = builder.advancedVSeed;
    if (!advancedVSeed || isTable(builder.vseed) || isPivotTable(builder.vseed)) return {};
    const { datasetReshapeInfo } = advancedVSeed;
    const colorIdMap = datasetReshapeInfo.reduce((prev, cur)=>({
            ...prev,
            ...cur.unfoldInfo.colorIdMap
        }), {});
    return colorIdMap;
};
const getColorValueMap = (builder)=>{
    const advancedVSeed = builder.advancedVSeed;
    if (!advancedVSeed || isTable(builder.vseed) || isPivotTable(builder.vseed) || isLinearColor(advancedVSeed, builder.vseed)) return;
    const { datasetReshapeInfo, chartType } = advancedVSeed;
    const unfoldInfo = datasetReshapeInfo[0]?.unfoldInfo;
    const baseConfig = advancedVSeed.config?.[chartType];
    const colorConfig = baseConfig?.color;
    if (!unfoldInfo || !colorConfig) return;
    const colorItems = unfoldInfo.colorItems ?? [];
    const colorIdMap = unfoldInfo.colorIdMap;
    const colorScheme = colorConfig.colorScheme ?? [];
    const specified = createSpecifiedForColorMapping(colorConfig.colorMapping, colorIdMap, colorItems) ?? {};
    return colorItems.reduce((prev, colorId, index)=>{
        const colorValue = specified[colorId] ?? colorScheme[index % colorScheme.length];
        if (colorValue) prev[colorId] = colorValue;
        return prev;
    }, {});
};
export { getColorIdMap, getColorItems, getColorValueMap };

//# sourceMappingURL=colorItems.js.map