"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _mobx = require("mobx");
var _services = _interopRequireDefault(require("../../services"));
var _utils = require("../../utils");
var _AppStore = _interopRequireDefault(require("../AppStore"));
var _lodash = _interopRequireDefault(require("lodash"));
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 ListChatStore {
  constructor() {
    _defineProperty(this, "isLoading", false);
    _defineProperty(this, "isLoadingPin", false);
    _defineProperty(this, "isLoadingMore", false);
    _defineProperty(this, "canLoadMore", true);
    _defineProperty(this, "isError", false);
    _defineProperty(this, "error", 0);
    _defineProperty(this, "page", 0);
    _defineProperty(this, "search", '');
    _defineProperty(this, "data", []);
    _defineProperty(this, "dataPin", []);
    _defineProperty(this, "orderStatusList", []);
    (0, _mobx.makeAutoObservable)(this);
  }
  async getData(params, onSuccess, onError) {
    try {
      if ((this.isLoading || this.isLoadingMore) && this.page !== 0) {
        return;
      }
      this.page += 1;
      params = {
        ...params,
        ...{
          search: this.search,
          page: this.page
        }
      };
      if (this.page === 1) {
        this.data = [];
        this.dataPin = [];
        this.isLoading = true;
        this.canLoadMore = true;
        if (_AppStore.default.appId !== 'Admin') this.getConversationPin(params);
      } else {
        if (!this.canLoadMore) {
          return;
        }
        this.isLoadingMore = true;
      }
      let response;
      if (_AppStore.default.appId === 'Admin') {
        response = await _services.default.create().getConversationsAdmin(params);
      } else {
        response = await _services.default.create().getConversations(params);
      }
      (0, _utils.Log)(response);
      if (response.status === 200) {
        if (response.data.status === 200) {
          if (response.data.data) {
            if (this.page === 1) {
              this.data = response.data.data;
            } else {
              this.data = [...this.data, ...response.data.data];
            }
            this.canLoadMore = response.data.data.length;
            this.isLoading = false;
            this.isLoadingMore = false;
            this.isError = false;
            if (onSuccess) {
              onSuccess(this.data);
            }
          }
        }
      } else {
        this.isLoading = false;
        this.isLoadingMore = false;
        this.isError = true;
        if (onError) {
          onError('');
        }
      }
    } catch (error) {
      this.isLoading = false;
      this.isLoadingMore = false;
      this.isError = true;
      if (onError) {
        onError(JSON.stringify(error));
      }
      (0, _utils.Log)(error);
    }
  }
  async getDataBackground() {
    try {
      _services.default.create().getConversations({
        page: 1
      }).then(response => {
        if (response.status === 200) {
          if (response.data.status === 200) {
            if (response.data.data) {
              this.data = [..._lodash.default.unionBy(response.data.data, (0, _mobx.toJS)(this.data), "_id")];
            }
          }
        }
      });
    } catch (error) {}
    try {
      _services.default.create().getConversationPin({
        page: 1
      }).then(response => {
        if (response.status === 200) {
          if (response.data.status === 200) {
            if (response.data.data) {
              this.dataPin = _lodash.default.unionBy(response.data.data, (0, _mobx.toJS)(this.dataPin), "_id");
            }
          }
        }
      });
    } catch (error) {}
  }
  async pin(params, onSuccess, onError) {
    const response = await _services.default.create().conversationPin(params);
    (0, _utils.Log)(response);
    if (response.status === 200 || response.status === 201) {
      if (response.data.status === 200) {
        if (onSuccess) onSuccess();
        return;
      }
    }
  }
  async unPin(params, onSuccess, onError) {
    const response = await _services.default.create().conversationUnPin(params);
    (0, _utils.Log)(response);
    if (response.status === 200 || response.status === 201) {
      if (response.data.status === 200) {
        if (onSuccess) onSuccess();
        return;
      }
    }
  }
  async mute(params, onSuccess, onError) {
    const response = await _services.default.create().conversationMute(params);
    (0, _utils.Log)(response);
    if (response.status === 200 || response.status === 201) {
      if (response.data.status === 200) {
        if (onSuccess) onSuccess();
      }
    }
  }
  async hide(params, onSuccess, onError) {
    const response = await _services.default.create().conversationHide(params);
    (0, _utils.Log)(response);
    if (response.status === 200 || response.status === 201) {
      if (response.data.status === 200) {
        if (onSuccess) onSuccess();
      }
    }
  }
  async getConversationPin(params) {
    this.isLoadingPin = true;
    const response = await _services.default.create().getConversationPin(params);
    (0, _utils.Log)(response);
    this.isLoadingPin = false;
    if (response.status === 200 || response.status === 201) {
      if (response.data.status === 200) {
        this.dataPin = _lodash.default.orderBy(response.data.data, c => {
          var _c$message;
          return c === null || c === void 0 ? void 0 : (_c$message = c.message) === null || _c$message === void 0 ? void 0 : _c$message.created_at;
        }, "desc");
        this.data = _lodash.default.orderBy(this.data, c => {
          var _c$message2;
          return c === null || c === void 0 ? void 0 : (_c$message2 = c.message) === null || _c$message2 === void 0 ? void 0 : _c$message2.created_at;
        }, "desc");
      }
    }
  }
  async getOrderStatus(params) {
    const response = await _services.default.create().getOrderStatus({});
    (0, _utils.Log)(response);
    if (response.status === 200 || response.status === 201) {
      if (response.data.status === 200) {
        this.orderStatusList = response.data.data;
      }
    }
  }
  getOrderStatusById(order_status) {
    if (this.orderStatusList && this.orderStatusList.length > 0) {
      const status = this.orderStatusList.find(status => status.status + '' === order_status + '');
      if (status) {
        return status.name;
      }
    }
    return (0, _utils.orderStatus)(order_status);
  }
}
const listChatStore = new ListChatStore();
var _default = listChatStore;
exports.default = _default;
//# sourceMappingURL=ListChatStore.js.map