import { TradeMode } from "@/common/constants/enumCommon"
import { getMarketByTradeMode } from "@/services/bus/market"
import { ref } from "vue"
import { queryTjmdTodayAccountMargin } from "../services/go/Tjmd"
import { QueryTjmdTodayAccountMarginReq, TjmdTodayAccountMarginReq } from "../services/go/Tjmd/interface"


// 注意：这个 保证金和 仓单贸易 里的保证金不一样
// 目前是掉期市场
export const useTodayMargin = (param: QueryTjmdTodayAccountMarginReq) => {
    // 查询 今日保证金
    const todayMargin = ref<TjmdTodayAccountMarginReq[]>([])
    queryTjmdTodayAccountMargin(param).then(res => {
        todayMargin.value = res
    })
    // 通过 市场 ID 查找 保证金信息
    function findTodayMarginByMarketId(marketid: number) {
        return todayMargin.value.find(e => e.marketid === marketid)
    }

    // 获取 结算保证金值
    function getReckonMarginValueByTradeMode(trademode: TradeMode) {
        let result = null
        const marketInfo = getMarketByTradeMode(trademode)
        if (marketInfo) {
            const temp = findTodayMarginByMarketId(marketInfo.marketid)?.infoc?.ReckonMarginValue
            result = temp ? temp : null
        }
        return result
    }
    // 获取 保证金值 方式
    function getReckonMarginTypeAndValueByTradeMode(trademode: TradeMode) {
        const result = [0, 0]
        const marketInfo = getMarketByTradeMode(trademode)
        if (marketInfo) {
            const temp = findTodayMarginByMarketId(marketInfo.marketid)?.infoc
            if (temp) {
                result[0] = temp.MarginAlgorithm
                result[1] = temp.MarketMarginValue
            }
        }
        return result
    }

    return { findTodayMarginByMarketId, getReckonMarginValueByTradeMode, getReckonMarginTypeAndValueByTradeMode }
}