import { ref, computed } from 'vue'
import { useDataset } from './dataset'
import { useOptions } from './options'
import { EchartsDataset } from './interface'

export function useLineChart() {
    const { dataset, clearData } = useDataset()
    const { options, initOptions } = useOptions(dataset)
    const dataIndex = ref(-1); // 当前数据索引值

    // 当前选中的数据项
    const selectedItem = computed(() => {
        const { price } = dataset.line.source;
        return {
            price: price[dataIndex.value] ?? '--',
        }
    })

    /**
     * 初始化数据
     */
    const initData = (data: EchartsDataset['line']['source']) => {
        clearData()
        dataIndex.value = -1;
        dataset.line.source = data
    }

    return {
        options,
        dataIndex,
        selectedItem,
        initData,
        initOptions,
    }
}