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 { observable, action, makeAutoObservable } from 'mobx';
import services, { getHeader } from "../../services";
import { Log } from "../../utils";
import uuid from "react-native-uuid";
import ImageResizer from "../../components/resizeImage";
import uploadProgress from './uploadProgress';
class ChatStore {
  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, "tab", 1);
    _defineProperty(this, "conversation_id", '');
    _defineProperty(this, "input", '');
    _defineProperty(this, "keyboardEmoji", false);
    _defineProperty(this, "data", []);
    _defineProperty(this, "images", []);
    _defineProperty(this, "queue", []);
    _defineProperty(this, "showAttachModal", false);
    _defineProperty(this, "location", {
      latitude: 0,
      longitude: 0
    });
    _defineProperty(this, "tabImage", void 0);
    _defineProperty(this, "tabQuickMessage", void 0);
    _defineProperty(this, "tabLocationMessage", void 0);
    _defineProperty(this, "quote", void 0);
    _defineProperty(this, "intervalSound", void 0);
    _defineProperty(this, "pauseSound", void 0);
    _defineProperty(this, "delay", delayInms => {
      return new Promise(resolve => setTimeout(resolve, delayInms));
    });
    makeAutoObservable(this);
  }
  onChangeTextChat(emoji) {
    this.input += emoji;
  }
  resetData() {
    this.isLoading = false;
    this.isLoadingMore = false;
    this.canLoadMore = true;
    this.isError = false;
    this.error = 0;
    this.page = 0;
    this.tab = 1;
    this.conversation_id = '';
    this.input = '';
    this.data = [];
    this.images = [];
    this.showAttachModal = false;
  }
  async getData(params, onSuccess, onError) {
    this.conversation_id = params.conversation_id;
    try {
      if (this.page !== 0 && (this.isLoading || this.isLoadingMore || !this.canLoadMore)) {
        return;
      }
      this.page += 1;
      if (this.page === 1) {
        this.isLoading = true;
        this.canLoadMore = false;
      } else {
        this.isLoadingMore = true;
      }
      const response = await services.create().conversationMessages({
        ...params,
        ...{
          page: this.page
        }
      });
      Log(response);
      if (response.status === 200) {
        if (response.data.status === 200) {
          if (response.data.data) {
            if (this.page === 1) {
              this.data = response.data.data;
              if (chatStore.quote !== undefined) {
                chatStore.data.unshift(chatStore.quote);
              }
            } else {
              this.data = [...this.data, ...response.data.data];
            }
            this.isLoading = false;
            this.isLoadingMore = false;
            this.canLoadMore = response.data.data.length > 0;
            this.isError = false;
            if (onSuccess) {
              onSuccess(this.data);
            }
          }
        } else {
          alert(response.data.message);
        }
      } 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));
      }
      Log(error);
    }
  }
  async sendMessage(params) {
    const quoteCopy = {
      ...this.quote
    };
    if (this.quote) {
      this.quote = undefined;
      await this.sendMessage(quoteCopy);
    }
    Log(params);
    let attachment_ids = [];
    if (params.attachmentLocal) {
      const formData = new FormData();
      for (let i = 0; i < params.attachmentLocal.length; i++) {
        var _extension;
        let extension = params.attachmentLocal[i].extension;
        if (params.attachmentLocal[i].name == null) {
          extension = params.attachmentLocal[i].uri.split(".").pop().toLowerCase();
        } else {
          extension = params.attachmentLocal[i].name.split(".").pop().toLowerCase();
        }
        extension = (_extension = extension) === null || _extension === void 0 ? void 0 : _extension.toLowerCase();
        let fileUri = '';
        if (extension === 'mov' || extension === 'mp4') {
          fileUri = params.attachmentLocal[i].uri;
          formData.append("files", {
            name: uuid.v4() + '.' + extension,
            uri: fileUri,
            type: 'video/' + extension
          });
        } else if (extension === 'doc' || extension === 'pdf' || extension === 'xls') {
          try {
            fileUri = params.attachmentLocal[i].uri;
            formData.append("files", {
              name: uuid.v4() + '.' + extension,
              uri: fileUri,
              type: params.attachmentLocal[i].type
            });
          } catch (e) {}
        } else if (extension === 'jpg' || extension === 'png' || extension === 'jpeg' || extension === 'heic') {
          let absolutePath = params.attachmentLocal[i].uri;
          // if (Platform.OS === 'android'){

          if (params.attachmentLocal[i].width >= 900 || params.attachmentLocal[i].height >= 900) {
            try {
              const result = await ImageResizer.createResizedImage(absolutePath, 900, 900, 'JPEG', 80, 0);
              fileUri = result.uri;
            } catch (e) {
              fileUri = absolutePath;
              console.log(e);
            }
          } else {
            fileUri = absolutePath;
          }
          try {
            formData.append("files", {
              name: uuid.v4() + '.' + extension,
              uri: fileUri,
              type: 'image/' + extension
            });
          } catch (e) {}
        } else {
          try {
            formData.append("files", {
              name: params.attachmentLocal[i].name,
              uri: params.attachmentLocal[i].uri,
              type: 'image/' + extension
            });
          } catch (e) {}
        }
      }
      const response = await services.create().uploadFile(formData, progress => {
        uploadProgress.progress[params.id] = Math.round(progress.loaded / progress.total * 100);
      });
      try {
        if (response.data.data) {
          attachment_ids = response.data.data.map(a => a._id);
        }
      } catch (e) {
        if (response.status === 201) {
          return;
        }
        this.data = this.data.map(item => {
          if (item.id === params.id && item.id) {
            item.status = 'error';
          }
          return item;
        });
        return;
      }
    }
    const response = await services.create().sendMessage({
      ...params,
      ...{
        attachment_ids: attachment_ids
      }
    });
    Log(response);
    if (response.status === 201 && response.data.status === 200) {
      this.data = [...this.data.map(item => {
        if (item.id === params.id && item.id) {
          var _response$data$data, _response$data$data$m, _response$data$data2, _response$data$data2$, _response$data$data3, _response$data$data3$;
          item.status = 'sent';
          item.attachmentLocal = [];
          item.attachments = (_response$data$data = response.data.data) !== null && _response$data$data !== void 0 && (_response$data$data$m = _response$data$data.message) !== null && _response$data$data$m !== void 0 && _response$data$data$m.attachments ? (_response$data$data2 = response.data.data) === null || _response$data$data2 === void 0 ? void 0 : (_response$data$data2$ = _response$data$data2.message) === null || _response$data$data2$ === void 0 ? void 0 : _response$data$data2$.attachments : [];
          // item.id = undefined
          item._id = (_response$data$data3 = response.data.data) === null || _response$data$data3 === void 0 ? void 0 : (_response$data$data3$ = _response$data$data3.message) === null || _response$data$data3$ === void 0 ? void 0 : _response$data$data3$._id;
        }
        return item;
      })];
    } else {
      this.data = this.data.map(item => {
        if (item.id === params.id && item.id) {
          item.status = 'error';
        }
        return item;
      });
    }
  }
}
const chatStore = new ChatStore();
export default chatStore;
//# sourceMappingURL=ChatStore.js.map