"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _mobx = require("mobx");
var _services = _interopRequireDefault(require("../services"));
var MyAsyncStorage = _interopRequireWildcard(require("../utils/MyAsyncStorage"));
var _socket = _interopRequireDefault(require("../socket"));
var _vi = _interopRequireDefault(require("../locales/vi.json"));
var _en = _interopRequireDefault(require("../locales/en.json"));
var _utils = require("../utils");
var _reactNative = require("react-native");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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 AppStore {
  constructor() {
    _defineProperty(this, "componentId", null);
    _defineProperty(this, "user", {});
    _defineProperty(this, "isLoading", false);
    _defineProperty(this, "isError", false);
    _defineProperty(this, "error", 0);
    _defineProperty(this, "data", {});
    _defineProperty(this, "lang", _vi.default);
    _defineProperty(this, "env", "DEV");
    _defineProperty(this, "appId", "VTPost");
    _defineProperty(this, "createConversationLoading", false);
    _defineProperty(this, "createConversationError", false);
  }
  //VTMan || VTPost

  changeLanguage(lang) {
    this.lang = lang === 'EN' ? _en.default : _vi.default;
  }
  async Auth(params, onSuccess, onError) {
    try {
      this.isLoading = true;
      const response = this.appId === 'VTMan' ? await _services.default.create().authVTM(params) : await _services.default.create().authVTP(params);
      (0, _utils.Log)(response);
      this.isLoading = false;
      if (response.status === 201) {
        if (response.data.status === 200) {
          if (response.data.data) {
            this.user = response.data.data;
            await MyAsyncStorage.save(MyAsyncStorage.USER, {
              ...response.data.data,
              ...{
                client_token: params.token
              }
            });
            await _socket.default.init();
            this.isError = false;
            if (onSuccess) {
              onSuccess(response.data.data);
            }
          }
        } else {
          alert(response.data.message);
        }
      } else {
        this.isLoading = false;
        this.isError = true;
        if (onError) {
          onError('');
        }
      }
    } catch (error) {
      this.isLoading = false;
      this.isError = true;
      if (onError) {
        onError(JSON.stringify(error));
      }
      (0, _utils.Log)(error);
    }
  }
  async createConversation(params, onSuccess, onError) {
    try {
      let response;
      if (appStore.appId === 'VTPost') {
        response = await _services.default.create().createConversationVTM(params);
      } else {
        response = await _services.default.create().createConversationVTP(params);
      }
      (0, _utils.Log)(response);
      this.createConversationLoading = false;
      if (response.status === 201) {
        if (response.data.status === 200) {
          if (response.data.data) {
            if (onSuccess) {
              onSuccess(response.data.data);
            }
          }
        } else {
          if (onError) {
            var _response$data;
            onError((_response$data = response.data) === null || _response$data === void 0 ? void 0 : _response$data.message);
          } else {
            alert(response.data.message);
          }
        }
      } else {
        this.createConversationLoading = false;
        this.createConversationError = true;
        if (onError) {
          onError('');
        }
      }
    } catch (error) {
      this.createConversationLoading = false;
      this.createConversationError = true;
      if (onError) {
        onError(error);
      }
      (0, _utils.Log)(error);
    }
  }
  async createConversationWithReceiver(params, onSuccess, onError) {
    try {
      let response;
      if (appStore.appId === 'VTPost') {
        response = await _services.default.create().vtpConversationWithReceiver(params);
      } else {
        response = await _services.default.create().vtmConversationWithReceiver(params);
      }
      (0, _utils.Log)(response);
      if (response.status === 201 || response.status === 200) {
        if (response.data.status === 200) {
          if (response.data.data) {
            if (onSuccess) {
              onSuccess(response.data.data);
            }
          }
        } else {
          if (response.data.status === 1006) {
            _reactNative.Alert.alert('Thông báo', 'Số điện thoại của người nhận chưa có tài khoản tại ViettelPost.', [{
              text: 'Đồng ý'
            }]);
            return;
          }
          if (onError) {
            var _response$data2;
            onError((_response$data2 = response.data) === null || _response$data2 === void 0 ? void 0 : _response$data2.message);
          } else {
            alert(response.data.message);
          }
        }
      } else {
        if (onError) {
          onError('');
        }
      }
    } catch (error) {
      if (onError) {
        onError(error);
      }
      (0, _utils.Log)(error);
    }
  }
  async createConversationWithCS(params, onSuccess, onError) {
    try {
      let response;
      if (appStore.appId === 'VTPost') {
        response = await _services.default.create().vtpConversationWithCS(params);
      } else {
        response = await _services.default.create().vtmConversationWithCS(params);
      }
      (0, _utils.Log)(response);
      if (response.status === 201 || response.status === 200) {
        if (response.data.status === 200) {
          if (response.data.data) {
            if (onSuccess) {
              onSuccess(response.data.data);
            }
          } else {
            alert(response.data.message);
          }
        } else {
          if (onError) {
            var _response$data3;
            onError((_response$data3 = response.data) === null || _response$data3 === void 0 ? void 0 : _response$data3.message);
          } else {
            alert(response.data.message);
          }
        }
      } else {
        if (onError) {
          onError('Có lỗi xảy ra. Vui lòng thử lại');
        }
      }
    } catch (error) {
      if (onError) {
        onError(error);
      }
      (0, _utils.Log)(error);
    }
  }
  async onlineState() {
    try {
      const response = await _services.default.create().onlineState({
        time: new Date().getTime()
      });
      (0, _utils.Log)(response);
    } catch (error) {}
  }
  async orderValidate(params, onSuccess, onError) {
    try {
      const response = await _services.default.create().orderValidate(params);
      (0, _utils.Log)(response);
      if (response.data.status === 200) {
        onSuccess();
      } else {
        onError(response.data.message);
      }
    } catch (error) {}
  }
  async loginAdmin(params, onSuccess, onError) {
    try {
      const response = await _services.default.create().loginAdmin(params);
      (0, _utils.Log)(response);
      if (response.data.status === 200) {
        response.data.data.user_id = response.data.data._id;
        this.user = response.data.data;
        onSuccess(response.data.data);
      } else {
        alert(response.data.message);
      }
    } catch (error) {
      alert(error);
    }
  }
  async loginVTP(params, onSuccess, onError) {
    try {
      const response = await _services.default.create().loginVTP({
        ...params,
        ...{
          "clientId": "appvtp",
          "clientSecret": "vtp-appvtp"
        }
      });
      (0, _utils.Log)(response);
      if (response.status === 200) {
        const responseClient = await _services.default.create().loginVTPClient({
          "Source": 2,
          "Key": "tw498twe89th238Ar5y98erfwsbvsagsdfepiafklE98rbhq38r543890314123rusdf87bq38972438r4378461237eryqwrf9b",
          "TokenSSO": response.data.accessToken,
          "refreshToken": "0f97f1f484f4dfa7fb4084340cdf643ec29cc5e80bc162049fe1cf267fe26934",
          "TokenIdSSO": "",
          "Type": "VTP"
        });
        (0, _utils.Log)(responseClient);
        if (responseClient.status === 200) {
          responseClient.data.data.TokenSSO = response.data.accessToken;
          onSuccess(responseClient.data.data);
        } else {
          alert(responseClient.data.errorMessage);
        }
      } else {
        alert(response.data.message);
      }
    } catch (error) {
      alert(error);
    }
  }
  async loginVTM(params, onSuccess, onError) {
    try {
      const response = await _services.default.create().loginVTM({
        ...params,
        ...{
          "source": -10,
          "latitude": 0,
          "longtitude": 0,
          "DeviceID": "string",
          "uniqueID": "string"
        }
      });
      (0, _utils.Log)(response);
      if (response.status === 200) {
        onSuccess(response.data.data);
      } else {
        alert(response.data.message);
      }
    } catch (error) {
      alert(error);
    }
  }
  async conversationDetail(params, onSuccess, onError) {
    try {
      const response = await _services.default.create().getConversationDetail(params);
      (0, _utils.Log)(response);
      if (response.status === 200) {
        onSuccess(response.data.data);
      } else {
        alert(response.data.message);
      }
    } catch (error) {
      alert(error);
    }
  }
  async vtpWithCS(params, onSuccess, onError) {
    try {
      const response = await _services.default.create().vtpConversationWithCS(params);
      (0, _utils.Log)(response);
      if (response.status === 200 || response.status === 201) {
        onSuccess(response.data.data);
      } else {
        alert(response.data.message);
      }
    } catch (error) {
      alert(error);
    }
  }
  async vtmWithCS(params, onSuccess, onError) {
    try {
      const response = await _services.default.create().vtmConversationWithCS(params);
      (0, _utils.Log)(response);
      if (response.status === 200 || response.status === 201) {
        onSuccess(response.data.data);
      } else {
        alert(response.data.message);
      }
    } catch (error) {
      alert(error);
    }
  }
}
const appStore = new AppStore();
var _default = appStore;
exports.default = _default;
//# sourceMappingURL=AppStore.js.map