import {wsClient} from './client'

type Handler = (msg: any) => void

const handlerMap: Record<string, Handler[]> = {}


// 注册 handler
export function registerHandler(type: string, handler: Handler) {
    if (!handlerMap[type]) {
        handlerMap[type] = []
    }
    handlerMap[type].push(handler)
}

// 初始化监听
export function initWsHandler() {
    wsClient.onMessage((msg) => {
        const handlers = handlerMap[msg.type]

        if (handlers && handlers.length > 0) {
            handlers.forEach(h => h(msg))
        } else {
            console.warn('[WS] 未处理消息:', msg.type, msg)
        }
    })
}