import { createFormatterByMeasure, isMeasure } from "../../../../utils/index.js";
const measureTreeToColumns = (spec, context)=>{
    const { advancedVSeed } = context;
    const measureTree = advancedVSeed.measureTree;
    const result = {
        ...spec
    };
    const eachNode = (node)=>{
        if (isMeasure(node)) return {
            width: 'auto',
            fieldFormat: fieldFormat(node)
        };
        return {};
    };
    const columns = treeTreeToColumns(measureTree, eachNode);
    return {
        ...result,
        columns: [
            ...result.columns || [],
            ...columns
        ]
    };
};
const fieldFormat = (node)=>{
    const formatter = createFormatterByMeasure(node);
    return (datum)=>{
        const { id } = node;
        const value = datum[id];
        return formatter(value);
    };
};
const treeTreeToColumns = (tree, callback)=>{
    const result = tree.map((item)=>{
        if ('children' in item && Array.isArray(item.children)) {
            const groupNode = item;
            const field = groupNode.id;
            const title = groupNode.alias ?? groupNode.id;
            const props = callback?.(groupNode) || {};
            return {
                field,
                title,
                columns: treeTreeToColumns(item.children, callback),
                ...props
            };
        }
        {
            const field = item.id;
            const title = item.alias ?? item.id;
            const props = callback?.(item) || {};
            return {
                field,
                title,
                ...props
            };
        }
    });
    return result || [];
};
export { measureTreeToColumns };

//# sourceMappingURL=measuresToColumns.js.map