"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _socket = _interopRequireDefault(require("socket.io-client"));
var _MyAsyncStorage = require("../utils/MyAsyncStorage");
var _ListChatStore = _interopRequireDefault(require("../screens/listchat/ListChatStore"));
var _ChatStore = _interopRequireDefault(require("../screens/chat/ChatStore"));
var _utils = require("../utils");
var _mobx = require("mobx");
var _AppStore = _interopRequireDefault(require("../screens/AppStore"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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); }
class Socket {
  constructor() {
    _defineProperty(this, "URL", 'https://dev-receiverchat.viettelpost.vn');
    _defineProperty(this, "onConnect", () => {
      (0, _utils.Log)('socket onConnected');
    });
    _defineProperty(this, "onDisconnect", () => {
      (0, _utils.Log)('socket onDisconnect');
    });
    _defineProperty(this, "onUserMessage", event => {
      try {
        var _event$message2;
        (0, _utils.Log)(event);
        //Handler conversation
        (0, _mobx.runInAction)(() => {
          _ListChatStore.default.data = _ListChatStore.default.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.default.conversation_id === ((_event$message2 = event.message) === null || _event$message2 === void 0 ? void 0 : _event$message2.conversation_id)) {
          const message = _ChatStore.default.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);
          });
          (0, _utils.Log)(message);
          if (!message) {
            const right = (event === null || event === void 0 ? void 0 : event.message.sender) === _AppStore.default.user.type + '_' + _AppStore.default.user.user_id;
            (0, _mobx.runInAction)(() => {
              if (!right) {
                _ChatStore.default.data.unshift(event.message);
                _ChatStore.default.data = [..._ChatStore.default.data];
              }
            });
          }
        }
      } catch (e) {
        (0, _utils.Log)(e);
      }
    });
  }
  async init() {
    const user = await (0, _MyAsyncStorage.load)(_MyAsyncStorage.USER);
    (0, _utils.Log)('socket', user);
    if (user && !this.socket) {
      this.socket = _socket.default.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();
var _default = mSocket;
exports.default = _default;
//# sourceMappingURL=index.js.map