"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");
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 AttachsStore {
  constructor() {
    _defineProperty(this, "isLoading", false);
    _defineProperty(this, "isLoadingMore", false);
    _defineProperty(this, "canLoadMore", true);
    _defineProperty(this, "isError", false);
    _defineProperty(this, "error", 0);
    _defineProperty(this, "page", 0);
    _defineProperty(this, "currentTab", 0);
    _defineProperty(this, "data", []);
    (0, _mobx.makeAutoObservable)(this);
  }
  async getData(params, onSuccess, onError) {
    try {
      if ((this.isLoading || this.isLoadingMore) && this.page !== 0) {
        return;
      }
      this.page += 1;
      if (this.page === 1) {
        this.isLoading = true;
        this.canLoadMore = true;
        this.data = [];
      } else {
        if (!this.canLoadMore) {
          return;
        }
        this.isLoadingMore = true;
      }
      params = {
        ...params,
        ...{
          page: this.page,
          type: this.currentTab === 0 ? 'IMG_VIDEO' : 'OTHER_FILE'
        }
      };
      let response = await _services.default.create().getConversationAttachments(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.map(m => {
                return m.attachments;
              }).flat(3);
            } else {
              this.data = [...this.data, ...response.data.data.map(m => {
                return m.attachments;
              }).flat(3)];
            }
            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);
    }
  }
}
const attachsStore = new AttachsStore();
var _default = attachsStore;
exports.default = _default;
//# sourceMappingURL=AttachsStore.js.map