import { unique } from "remeda";
import { findAllMeasures } from "../../../../utils/index.js";
const encodingForPivotTable = (advancedVSeed)=>{
    const { measureTree = [], dimensionTree = [] } = advancedVSeed;
    const measures = findAllMeasures(measureTree);
    const hasDimensionEncoding = dimensionTree.some((item)=>item.encoding);
    const hasMeasureEncoding = measures.some((item)=>item.encoding);
    const encoding = {};
    if (hasDimensionEncoding) generateDimensionEncoding(dimensionTree, encoding);
    else generateDefaultDimensionEncoding(dimensionTree, encoding);
    if (hasMeasureEncoding) generateMeasureEncoding(measures, encoding);
    else generateDefaultMeasureEncoding(measures, encoding);
    return {
        ...advancedVSeed,
        encoding
    };
};
const generateDefaultDimensionEncoding = (dimensions, encoding)=>{
    const uniqueDimIds = unique(dimensions.map((d)=>d.id));
    encoding.tooltip = uniqueDimIds;
    encoding.row = [];
    encoding.column = [];
    uniqueDimIds.forEach((item, index)=>{
        if (index % 2 === 0) encoding.column.push(item);
        else encoding.row.push(item);
    });
};
const generateDimensionEncoding = (dimensions, encoding)=>{
    const uniqueDimIds = unique(dimensions.map((d)=>d.id));
    encoding.tooltip = uniqueDimIds;
    encoding.row = [];
    encoding.column = [];
    dimensions.forEach((dim, index)=>{
        const id = dim.id;
        if ('row' === dim.encoding) encoding.row.push(id);
        else if ('column' === dim.encoding) encoding.column.push(id);
        else if (index % 2 === 0) encoding.column.push(id);
        else encoding.row.push(id);
    });
};
const generateDefaultMeasureEncoding = (measures, encoding)=>{
    encoding.tooltip = unique(measures.map((item)=>item.id));
    encoding.detail = unique(measures.filter((item)=>'detail' === item.encoding || !item.encoding).map((item)=>item.id));
};
const generateMeasureEncoding = (measures, encoding)=>{
    encoding.tooltip = measures.map((item)=>item.id);
    encoding.detail = unique(measures.filter((item)=>'detail' === item.encoding || !item.encoding).map((item)=>item.id));
};
export { encodingForPivotTable };

//# sourceMappingURL=pivotTable.js.map