import { unique } from "remeda";
import { MeasureId } from "../../../../../dataReshape/index.js";
import { hasMultipleMeasureInSingleView } from "../../../../utils/index.js";
const defaultEncodingForHierarchy = (advancedVSeed)=>{
    const { measures = [], reshapeMeasures = [], dimensions = [] } = advancedVSeed;
    const encoding = {};
    generateDefaultDimensionEncoding(dimensions, encoding, hasMultipleMeasureInSingleView(reshapeMeasures));
    generateDefaultMeasureEncoding(measures, encoding);
    return {
        ...advancedVSeed,
        encoding
    };
};
const encodingForHierarchy = (advancedVSeed)=>{
    const { measures = [], reshapeMeasures = [], dimensions = [] } = advancedVSeed;
    const hasDimensionEncoding = dimensions.some((item)=>item.encoding);
    const hasMeasureEncoding = measures.some((item)=>item.encoding);
    const encoding = {};
    const hasMulti = hasMultipleMeasureInSingleView(reshapeMeasures);
    if (hasDimensionEncoding) generateDimensionEncoding(dimensions, encoding, hasMulti);
    else generateDefaultDimensionEncoding(dimensions, encoding, hasMulti);
    if (hasMeasureEncoding) generateMeasureEncoding(measures, encoding);
    else generateDefaultMeasureEncoding(measures, encoding);
    return {
        ...advancedVSeed,
        encoding
    };
};
const generateDefaultDimensionEncoding = (dimensions, encoding, isMultiMeasure)=>{
    const uniqueDimIds = unique(dimensions.map((d)=>d.id));
    if (isMultiMeasure) encoding.hierarchy = uniqueDimIds;
    else encoding.hierarchy = uniqueDimIds.filter((d)=>d !== MeasureId);
    encoding.detail = encoding.hierarchy;
    encoding.tooltip = uniqueDimIds.filter((d)=>d !== MeasureId);
    encoding.label = [];
    encoding.row = [];
    encoding.column = [];
};
const generateDimensionEncoding = (dimensions, encoding, isMultiMeasure)=>{
    encoding.hierarchy = unique(dimensions.filter((item)=>'hierarchy' === item.encoding).map((item)=>item.id));
    if (isMultiMeasure && !encoding.hierarchy.includes(MeasureId)) encoding.hierarchy.push(MeasureId);
    encoding.color = encoding.hierarchy.slice(0, 1);
    encoding.detail = unique(dimensions.filter((item)=>'detail' === item.encoding).map((item)=>item.id));
    if (0 === encoding.detail.length) encoding.detail = encoding.hierarchy;
    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.row = unique(dimensions.filter((item)=>'row' === item.encoding).map((item)=>item.id));
    encoding.column = unique(dimensions.filter((item)=>'column' === item.encoding).map((item)=>item.id));
};
const generateDefaultMeasureEncoding = (measures, encoding)=>{
    encoding.size = unique(measures.filter((item)=>'size' === item.encoding || !item.encoding).map((item)=>item.id));
    encoding.tooltip = unique([
        ...encoding.tooltip || [],
        ...measures.map((m)=>m.id)
    ]);
};
const generateMeasureEncoding = (measures, encoding)=>{
    encoding.size = unique(measures.filter((item)=>'size' === item.encoding || !item.encoding).map((item)=>item.id));
    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,
        ...color
    ]);
};
export { defaultEncodingForHierarchy, encodingForHierarchy };

//# sourceMappingURL=hierarchy.js.map