import ChatMessageDB from '@/db/ChatMessageDB'
import { DisplayMessage, messageStore } from '@/store/messagesStore'
import { getUserMsg } from './userMsg'

export function getMasterIdFromDisplayMsg(msg: DisplayMessage) {
  if (!msg.state) {
    return msg.target as string
  } else {
    return msg.from as string
  }
}

export function getTargetIdFromDisplayMsg(msg: DisplayMessage) {
  if (!msg.state) {
    return msg.from as string
  } else {
    return msg.target as string
  }
}

export async function checkPendingMsgAndSendIt() {
  console.log('检查pending的消息，然后发送')
  const addr = getUserMsg()?.userInfo.addr
  if (addr) {
    const pendingMessageList = await ChatMessageDB.getInstance().getPendingMessage(
      addr,
    )

    pendingMessageList.forEach((i) => {
      messageStore.sendMessage({
        // ...i,
        type: i.type,
        content: i.content,
        target: i.target as string,
        uuid: i.uuid,
      })
    })
  }
}
