function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
import io from 'socket.io-client';
import { load, USER } from "../utils/MyAsyncStorage";
import listChatStore from "../screens/listchat/ListChatStore";
import chatStore from "../screens/chat/ChatStore";
import { Log } from "../utils";
import { runInAction } from "mobx";
import appStore from "../screens/AppStore";
class Socket {
  constructor() {
    _defineProperty(this, "URL", 'https://dev-receiverchat.viettelpost.vn');
    _defineProperty(this, "onConnect", () => {
      console.log('socket', 'onConnected');
      this.socket.on('USER_MESSAGE', this.onUserMessage);
      this.socket.on('USER_STATE', this.onUserStateMessage);
    });
    _defineProperty(this, "onDisconnect", () => {
      console.log('socket', 'onDisconnect');
    });
    _defineProperty(this, "onUserMessage", event => {
      console.log('socket', event);
      try {
        var _event$message3;
        const right = (event === null || event === void 0 ? void 0 : event.message.sender) === appStore.user.type + '_' + appStore.user.user_id;
        try {
          runInAction(() => {
            listChatStore.dataPin = [...listChatStore.dataPin.map(c => {
              var _event$message;
              if (c._id === (event === null || event === void 0 ? void 0 : (_event$message = event.message) === null || _event$message === void 0 ? void 0 : _event$message.conversation_id)) {
                if (!right) {
                  c.settings = c.settings.map(setting => {
                    setting.unread_count += 1;
                    return setting;
                  });
                }
                c.message = event.message;
              }
              return c;
            })];
            listChatStore.dataPin = [...listChatStore.dataPin];
          });
        } catch (e) {
          console.log(e);
        }

        //Handler conversation
        try {
          runInAction(() => {
            listChatStore.data = listChatStore.data.map(c => {
              var _event$message2;
              if (c._id === (event === null || event === void 0 ? void 0 : (_event$message2 = event.message) === null || _event$message2 === void 0 ? void 0 : _event$message2.conversation_id)) {
                if (!right) {
                  c.settings = c.settings.map(setting => {
                    setting.unread_count += 1;
                    return setting;
                  });
                }
                c.message = event.message;
              }
              return c;
            });
            listChatStore.data = [...listChatStore.data];
          });
        } catch (e) {
          console.log(e);
        }

        //Handler message

        if (chatStore.conversation_id === ((_event$message3 = event.message) === null || _event$message3 === void 0 ? void 0 : _event$message3.conversation_id)) {
          const message = chatStore.data.find(m => {
            var _event$message4;
            return m._id === (event === null || event === void 0 ? void 0 : (_event$message4 = event.message) === null || _event$message4 === void 0 ? void 0 : _event$message4._id);
          });
          if (!message && !right) {
            runInAction(() => {
              chatStore.data = [event.message, ...chatStore.data];
              chatStore.data = [...chatStore.data];
            });
          }
        }
      } catch (e) {
        Log(e);
      }
    });
    _defineProperty(this, "onUserStateMessage", event => {
      console.log('socket state', event);
      try {
        runInAction(() => {
          listChatStore.dataPin = [...listChatStore.dataPin.map(c => {
            if (c.sender === (event === null || event === void 0 ? void 0 : event.full_user_id)) {
              c.detail_participants = c.detail_participants.map(pa => {
                if (pa.full_user_id === event.full_user_id) {
                  pa['state'] = [event.state];
                }
                return pa;
              });
            }
            return c;
          })];
          listChatStore.dataPin = [...listChatStore.dataPin];
        });
      } catch (e) {
        console.log(e);
      }

      //Handler conversation
      try {
        runInAction(() => {
          listChatStore.data = listChatStore.data.map(c => {
            if (c.sender === (event === null || event === void 0 ? void 0 : event.full_user_id)) {
              c.detail_participants = c.detail_participants.map(pa => {
                if (pa.full_user_id === event.full_user_id) {
                  pa['state'] = [event.state];
                }
                console.log('pa', pa);
                return pa;
              });
            }
            return c;
          });
          listChatStore.data = [...listChatStore.data];
        });
      } catch (e) {
        console.log(e);
      }
    });
  }
  async init() {
    const user = await load(USER);
    Log(user);
    if (user && !this.socket) {
      this.socket = io.connect(this.URL, {
        reconnection: true,
        reconnectionDelay: 1000,
        reconnectionDelayMax: 9999999,
        transports: ['websocket'],
        extraHeaders: {
          Authorization: user.token
        }
      });
      this.socket.on('connect', this.onConnect);
      this.socket.on('disconnect', this.onDisconnect);
    } else {
      // alert('Kết nối Socket ko thành công.')
    }
  }
}
const mSocket = new Socket();
export default mSocket;
//# sourceMappingURL=index.js.map