import { MESSAGE_TARGET_EXTENSION, MESSAGE_TARGET_INJECT, MESSAGE_TO_EXTENSION_ROUTE_MSG } from '@/consts/const'
import { injectWaiter } from './useMessageService'
import { useCallback } from 'react'
import Layer1Protocol from './Layer1Protocol'

const useMessage = () => {
    const sendExtension = useCallback(async <T = any>(method: string, params?: any) => {
        // wait
        const portMessageHandler = await injectWaiter.wait() as Layer1Protocol<MessageData, MessageResult>
        // send message
        const messageResult = await portMessageHandler.sendMessage({
            from: 'app',
            target: MESSAGE_TARGET_EXTENSION,
            method,
            params: params ?? {},
        }) as MessageResult | undefined
        if (messageResult != null) {
            if (messageResult.success) {
                return messageResult.data as T
            } else {
                throw new Error(messageResult.message)
            }
        } else {
            throw new Error('no response')
        }
    }, [])

    const sendInject = useCallback(async <T = any>(method: string, params?: any): Promise<T> => {
        return await sendExtension(MESSAGE_TO_EXTENSION_ROUTE_MSG, {
            target: MESSAGE_TARGET_INJECT,
            method,
            params: params ?? {},
        })
    }, [])

    return {
        sendExtension,
        sendInject
    }
}

export default useMessage