import _ from 'lodash-es';
import { $echarts } from '../../index.js';
import { thousandFormat } from '../../libs/amount-format.js';

export default ({ dom, param, opt }) => {
    /**
     * 对外参数
     */
    const $data = param.data;
    const $param = {
        ...param,
        radius: param.radius,
        center: param.center,
    };

    /**
     * 导出配置项
     */
    let option = {
        legend: {
            show: true,
            type: 'scroll',
            bottom: 20,
            right: 'center',
            width: '90%',
            pageIconColor: '#fff',
            pageTextStyle: {
                color: '#fff'
            },
            itemWidth: 14,
            itemGap: 16,
            data: $data,
            formatter: (name) => {
                let res = '';
                $data.forEach((x) => {
                    if (x.name === name) {
                        res = `${name} {blue|${thousandFormat(x.value, 2)} ${x.unit || ''}}`;
                    }
                });
                return res;
            },
            textStyle: {
                color: '#fff',
                rich: {
                    blue: {
                        color: '#00DEFF',
                    },
                },
            },
        },
        series: [
            {
                type: 'pie',
                radius: $param.radius,
                center: $param.center,
                label: {
                    position: 'inside',
                    formatter: function (params) {
                        return `{percent|${params.percent}%}`;
                    },
                    rich: {
                        percent: {
                            color: '#fff',
                        },
                    },
                },
                startAngle: 0,
                roseType: 'area',
                data:  $data
            },
        ],
    };

    /**
     * 渲染
     */
    let extensOption = $echarts.extens(opt, option);

    let echarts = $echarts.render(dom, extensOption);

    return echarts
};
