

/**
 * @function 当children为空数组时，就删除children
 * @param {Array} obj - 待处理的数组
 * @returns {Array} - 处理后的数组
 */
export const removeEmptyChildren = (obj) => {
    if (Array.isArray(obj)) {
        // 如果是数组，遍历每个元素递归处理
        return obj.map(item => removeEmptyChildren(item));
    } else if (obj && typeof obj === 'object') {
        // 创建新对象
        const newObj = { ...obj };

        // 处理 children 属性
        if (
            Array.isArray(newObj.children) &&
            newObj.children.length === 0
        ) {
            delete newObj.children;
        } else if (newObj.children) {
            // 递归处理子节点
            newObj.children = removeEmptyChildren(newObj.children);
        }

        return newObj;
    }
    return obj;
};

/**
 * @function 构建出符合Echarts组件的配置项-只针对柱状图
 * @param {Array} list 
 * @returns {Object} echarts配置项
 */
export const buildEchartData = (list = []) => {
    const xAxis = {
        name: "地区",
        type: "category",
        axisTick: { show: false },
        data: list?.[0]?.xdata,
    };
    const yChartData = list?.map((item) => {
        return {
            name: item.lineName,
            data: item.data
        }
    })

    return {
        xAxis,
        yAxis: { name: "人数" },
        tooltip: {
            show: true,
        },
        yChartData,
    }
}

/**
 * @function 构建出符合Echarts组件的配置项-只针对柱状图和饼图
 * @param {Object} obj
 * @returns {Object} echarts配置项
 */
export const buildBarAndPieOption = (obj = {}) => {
    const numList = obj?.num || [];
    const rateList = obj?.rate || [];
    return {
        legend: { show: false },
        xAxis: {
            name: "",
            type: "category",
            axisTick: { show: false },
            data: numList?.[0]?.xdata || [],
        },
        yAxis: {
            type: "value",
        },
        tooltip: {
            show: true,
        },
        yChartData: [
            {
                name: "人数",
                data: numList?.[0]?.data || []
            }
        ],
        yChartDataPie: rateList?.[0]?.xdata?.map((item, index) => {
            return {
                name: item,
                data: [rateList?.[0]?.data?.[index]]
            }
        })
    }
}

