import { unique } from "remeda";
import { MeasureId } from "../../../../../dataReshape/index.js";
import { hasMultipleMeasureInSingleView } from "../../../../utils/index.js";
import { addColorToEncoding } from "./color/index.js";
const defaultEncodingForHeatmap = (advancedVSeed)=>{
    const { measures = [], dimensions = [] } = advancedVSeed;
    const encoding = {};
    generateDefaultDimensionEncoding(dimensions, encoding);
    generateDefaultMeasureEncoding(measures, encoding);
    return {
        ...advancedVSeed,
        encoding
    };
};
const encodingForHeatmap = (advancedVSeed)=>{
    const { measures = [], reshapeMeasures = [], dimensions = [] } = advancedVSeed;
    const hasDimensionEncoding = dimensions.some((item)=>item.encoding);
    const hasMeasureEncoding = measures.some((item)=>item.encoding);
    const encoding = {};
    if (hasDimensionEncoding) generateDimensionEncoding(dimensions, encoding, hasMultipleMeasureInSingleView(reshapeMeasures));
    else generateDefaultDimensionEncoding(dimensions, encoding);
    if (hasMeasureEncoding) generateMeasureEncoding(measures, encoding);
    else generateDefaultMeasureEncoding(measures, encoding);
    return {
        ...advancedVSeed,
        encoding
    };
};
const generateDefaultDimensionEncoding = (dimensions, encoding)=>{
    const onlyMeasureId = 1 === dimensions.length && dimensions.find((item)=>item.id === MeasureId);
    const uniqueDimIds = unique(dimensions.map((d)=>d.id));
    encoding.x = uniqueDimIds.slice(0, 1);
    encoding.y = uniqueDimIds.slice(onlyMeasureId ? 0 : 1);
    encoding.color = uniqueDimIds.slice(onlyMeasureId ? 0 : 1);
    encoding.detail = encoding.color;
    encoding.tooltip = uniqueDimIds.filter((d)=>d !== MeasureId);
    encoding.label = [];
    encoding.row = [];
    encoding.column = [];
};
const generateDimensionEncoding = (dimensions, encoding, isMultiMeasure)=>{
    encoding.x = unique(dimensions.filter((item)=>'xAxis' === item.encoding).map((item)=>item.id));
    if (0 === encoding.x.length) encoding.x = [
        dimensions[0].id
    ];
    encoding.y = unique(dimensions.filter((item)=>'yAxis' === item.encoding).map((item)=>item.id));
    if (0 === encoding.y.length) if (dimensions.length > 1) encoding.y = dimensions.slice(1).map((item)=>item.id);
    else encoding.y = dimensions.slice(0).map((item)=>item.id);
    addColorToEncoding(dimensions, encoding, isMultiMeasure);
    encoding.detail = unique(dimensions.filter((item)=>'detail' === item.encoding).map((item)=>item.id));
    if (0 === encoding.detail.length) encoding.detail = encoding.color;
    encoding.tooltip = unique(dimensions.map((item)=>item.id));
    encoding.tooltip = encoding.tooltip.filter((d)=>d !== MeasureId);
    encoding.label = unique(dimensions.filter((item)=>'label' === item.encoding).map((item)=>item.id));
    encoding.label = encoding.label.filter((d)=>d !== MeasureId);
};
const generateDefaultMeasureEncoding = (measures, encoding)=>{
    const colorDims = unique(measures.filter((item)=>'color' === item.encoding || !item.encoding).map((item)=>item.id));
    if (colorDims.length > 0) encoding.color = colorDims;
};
const generateMeasureEncoding = (measures, encoding)=>{
    const color = unique(measures.filter((item)=>'color' === item.encoding).map((item)=>item.id));
    if (color.length > 0) encoding.color = color;
    const label = unique(measures.filter((item)=>'label' === item.encoding).map((item)=>item.id));
    encoding.label = unique([
        ...encoding.label || [],
        ...label
    ]);
    const tooltip = unique(measures.filter((item)=>'tooltip' === item.encoding).map((item)=>item.id));
    encoding.tooltip = unique([
        ...encoding.tooltip || [],
        ...label,
        ...tooltip
    ]);
};
export { defaultEncodingForHeatmap, encodingForHeatmap };

//# sourceMappingURL=heatmap.js.map