import {listDictDataAll} from "../../api/system/dict/data";

const selections = {
  state: {
    SSEMessage: {},
    messageActivedSearch: false,
    sseMessageClientId: "",
    dict: {},
    SSEChatMessage:{},
    //
    SSEChatMessageClientId:"",
    //存储所有聊天室连接id
    chatRoomClientIds:[],
  },

  mutations: {
    SET_DICT: (state, data) => {
      state.dict = data
    },
    SET_SSEMESSAGE: (state, data) => {
      state.SSEMessage = data
    },
    SET_MESSAGE_ACTIVED_SEARCH: (state, data) => {
      state.messageActivedSearch = data
    },
    SET_MESSAGE_CLIENT_ID: (state, data) => {
      state.sseMessageClientId = data
    },
    SET_SSECHATMESSAGE: (state, data) => {
      state.SSEChatMessage = data
    },
    SET_SSECHATMESSAGE_CLIENT_ID: (state, data) => {
      state.SSEChatMessageClientId = data
    },
    SET_CHATROOMCLIENTIDS: (state, data) => {
      state.chatRoomClientIds = data
    }

  },

  actions: {
    loadingSSEChatMessage({commit}, messageObj){
      return new Promise((resolve, reject) => {
        commit('SET_SSECHATMESSAGE', messageObj)
        if(messageObj&&messageObj.chatRooms){
          let chatRoomClientIds=messageObj.chatRooms
            .map(item=>item.id);
          commit('SET_CHATROOMCLIENTIDS', chatRoomClientIds)
        }

      })
    },
    loadingSSEChatMessageClientId({commit}, clientId){
      return new Promise((resolve, reject) => {
        commit('SET_SSECHATMESSAGE_CLIENT_ID', clientId)
      })
    },
    //加载SSEMESSAGE
    loadingSSEMessage({commit}, messageObj) {
      return new Promise((resolve, reject) => {
        commit('SET_SSEMESSAGE', messageObj)
      })
    },
    //消息跳转,激活页面查询
    activedMessageSearch({commit}) {
      return new Promise((resolve, reject) => {
        commit('SET_MESSAGE_ACTIVED_SEARCH', true);
        resolve();
      })
    },
    disabledMessageSearch({commit}) {
      return new Promise((resolve, reject) => {
        commit('SET_MESSAGE_ACTIVED_SEARCH', false);
        resolve();
      })
    },
    // 加载字典数据
    GetDicts({commit}) {
      return new Promise((resolve, reject) => {
        listDictDataAll().then(res => {
          commit('SET_DICT', res.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
  }
}

export default selections
