import { dealStrCalculatorData } from '@/utils/dealData';
import { ref, unref } from 'vue';
import { EexpenseType } from '../components/FormulaList/types';

/**
 * 
 * @returns choice 选择计算符号
 * @returns generatingFormula 生成公式
 * @returns dealStrCalculatorData 反处理成公式
 * @returns clearCalculator 清除公式
 * @returns choiceList 选择的公式数据
 * }
 */
export function useFormulaList() {
    // 选中公式的数据
    const choiceList = ref<EexpenseType[]>([])

    // 选择对应符号
    function choice(params: EexpenseType) {
        if (!params.key) return;
        params.key.indexOf('<==') !== -1 ? choiceList.value.pop() : choiceList.value.push(params)
    }

    // 生成公式
    function generatingFormula() {
        const choiceList_ = unref(choiceList.value)
        if (choiceList_.length === 0) return [0, 0];
        let formulaStr = "";
        const length = choiceList.value.length;
        for (let index = 0; index < length; index++) {
            formulaStr += choiceList.value[index].value
        }
        clearCalculator()
        return [formulaStr, choiceList_]
    }

    // 处理字符串转为显示数组
    const dealFormulaStrCalculatorData = (countModel: Recordable, calculatorData: Recordable) => {
        choiceList.value = dealStrCalculatorData(countModel, calculatorData)
    }

    // 清除公式
    function clearCalculator() {
        choiceList.value = []
    }

    return { choice, generatingFormula, dealFormulaStrCalculatorData, clearCalculator, choiceList }
}