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,
    )

    for(let key in pendingMessageList){
      const i = pendingMessageList[key];
      // pendingMessageList.forEach(async (i) => {
        console.log(i.content, 'show content');
        
        await messageStore.sendMessage({
          // ...i,
          type: i.type,
          content: i.content,
          target: i.target as string,
          uuid: i.uuid,
        })
      // })
    }
  }
}
