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", () => {
      Log('socket onConnected');
    });
    _defineProperty(this, "onDisconnect", () => {
      Log('socket onDisconnect');
    });
    _defineProperty(this, "onUserMessage", event => {
      try {
        var _event$message2;
        Log(event);
        //Handler conversation
        runInAction(() => {
          listChatStore.data = listChatStore.data.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)) {
              c.message = event.message;
            }
            return c;
          });
        });

        //Handler message

        if (chatStore.conversation_id === ((_event$message2 = event.message) === null || _event$message2 === void 0 ? void 0 : _event$message2.conversation_id)) {
          const message = chatStore.data.find(m => {
            var _event$message3;
            return m._id === (event === null || event === void 0 ? void 0 : (_event$message3 = event.message) === null || _event$message3 === void 0 ? void 0 : _event$message3._id);
          });
          Log(message);
          if (!message) {
            const right = (event === null || event === void 0 ? void 0 : event.message.sender) === appStore.user.type + '_' + appStore.user.user_id;
            runInAction(() => {
              if (!right) {
                chatStore.data.unshift(event.message);
                chatStore.data = [...chatStore.data];
              }
            });
          }
        }
      } catch (e) {
        Log(e);
      }
    });
  }
  async init() {
    const user = await load(USER);
    Log('socket', user);
    if (user && !this.socket) {
      this.socket = io.connect(this.URL, {
        reconnection: true,
        reconnectionDelay: 1000,
        reconnectionDelayMax: 5000,
        transports: ['websocket'],
        extraHeaders: {
          Authorization: user.token
        }
      });
      this.socket.on('connect', this.onConnect);
      this.socket.on('disconnect', this.onDisconnect);
      this.socket.on('USER_MESSAGE', this.onUserMessage);
    }
  }
}
const mSocket = new Socket();
export default mSocket;
//# sourceMappingURL=index.js.map