import { $echarts } from '../../index.js';
import echartstyle from '../../libs/echarts-style.js';
import * as $switch from '../../extend/bar-simple.js';

export default ({ dom, param, opt }) => {
    const { $color, $tooltip, $grid } = echartstyle();

    const { data } = param;

    /**
     * 导出配置项
     */
    const option = {
        color: $color.theme,
        grid: $grid,
        tooltip: Object.assign(
            {
                trigger: 'axis',
                axisPointer: {
                    type: 'line',
                },
            },
            $tooltip
        ),
        legend: {
            icon: 'rect',
            top: 10,
            right: 20,
            itemWidth: 10,
            itemHeight: 3,
            itemGap: 15,
            textStyle: {
                color: $color.legendLabel,
                fontSize: 12,
            },
        },
        xAxis: {
            type: 'category',
            data: data.axis,
            axisTick: {
                show: false,
            },
            axisLabel: {
                show: false,
            },
            axisLine: {
                lineStyle: {
                    color: $color.xAxisLine,
                },
            },
            splitLine: {
                show: false,
                lineStyle: {
                    color: $color.yAxisLine,
                },
            },
        },
        yAxis: {
            type: 'value',
            axisTick: {
                show: false,
            },
            axisLabel: {
                show: false,
            },
            axisLine: {
                show: false,
            },
            splitLine: {
                lineStyle: {
                    color: $color.yAxisLine,
                },
            },
        },
        series: [],
    };

    /**
     * 数据处理
     */
    data.series.map((item) => {
        option.series.push({
            name: item.name,
            data: item.data,
            type: 'bar',
            barWidth: 15,
            emphasis: {
                focus: 'series',
            },
            label: {
                show: true,
                formatter: (param) => {
                    return data.axis[param.dataIndex];
                },
                position: 'left',
                offset: [55, -20],
            },
        });
    });

    /**
     * 插拔式开关（整块可移除，或者移除其中一项，也不会有任何影响）
     */
    $switch.handleDirection(param, option, data.axis); // 改变方向

    option.series = option.series.map((item, index) => {
        $switch.handleStack(param, item); // 堆叠
        $switch.handleBackground(param, item); // 背景色
        $switch.handleColor(param, item, index); // 颜色
        $switch.handleBorder(param, item, index, $color); // 边框
        $switch.handleBarWidth(param, item); // 柱状图尺寸

        return item;
    });

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

    const echarts = $echarts.render(dom, extensOption);
};
