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, runInAction } from 'mobx';
import services from "../services";
import * as MyAsyncStorage from "../utils/MyAsyncStorage";
import { USER } from "../utils/MyAsyncStorage";
import Socket from "../socket";
import vi from "../locales/vi.json";
import en from "../locales/en.json";
import { Log } from "../utils";
import { Alert } from 'react-native';
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);
    _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 : vi;
  }
  async Auth(params, onSuccess, onError) {
    try {
      this.isLoading = true;
      const response = this.appId === 'VTMan' ? await services.create().authVTM(params) : await services.create().authVTP(params);
      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(USER, {
              ...response.data.data,
              ...{
                client_token: params.token
              }
            });
            await Socket.getInstance().init();
            this.isError = false;
            if (onSuccess) {
              onSuccess(response.data.data);
            }
          }
        } else {
          var _response$data, _response$data2;
          Alert.alert('Thông báo', response !== null && response !== void 0 && (_response$data = response.data) !== null && _response$data !== void 0 && _response$data.message ? response === null || response === void 0 ? void 0 : (_response$data2 = response.data) === null || _response$data2 === void 0 ? void 0 : _response$data2.message : 'Có lỗi xảy ra.', [{
            text: 'Đồng ý'
          }]);
        }
      } else {
        this.isLoading = false;
        this.isError = true;
        // Alert.alert('Thông báo', response?.data?.message?response?.data?.message: 'Có lỗi xảy ra.', [{text: 'Đồng ý'}])
      }
    } catch (error) {
      this.isLoading = false;
      this.isError = true;
      // Alert.alert('Thông báo',  'Có lỗi xảy ra.', [{text: 'Đồng ý'}])
      Log(error);
    }
  }
  async createConversation(params, onSuccess, onError) {
    try {
      let response;
      if (appStore.appId === 'VTPost') {
        response = await services.create().createConversationVTM(params);
      } else {
        response = await services.create().createConversationVTP(params);
      }
      Log(response);
      this.createConversationLoading = false;
      if (response.status === 201 || response.status === 200) {
        if (response.data.status === 200 || response.data.status === 201) {
          if (response.data.data) {
            if (onSuccess) {
              onSuccess(response.data.data);
            }
          }
        } else {
          var _response, _response$data3, _response2, _response2$data;
          // if (onError) {
          //   onError(response.data?.message);
          // } else {
          Alert.alert('Thông báo', (_response = response) !== null && _response !== void 0 && (_response$data3 = _response.data) !== null && _response$data3 !== void 0 && _response$data3.message ? (_response2 = response) === null || _response2 === void 0 ? void 0 : (_response2$data = _response2.data) === null || _response2$data === void 0 ? void 0 : _response2$data.message : 'Có lỗi xảy ra.', [{
            text: 'Đồng ý'
          }]);
          // }
        }
      } else {
        var _response3, _response3$data, _response4, _response4$data;
        this.createConversationLoading = false;
        this.createConversationError = true;
        Alert.alert('Thông báo', (_response3 = response) !== null && _response3 !== void 0 && (_response3$data = _response3.data) !== null && _response3$data !== void 0 && _response3$data.message ? (_response4 = response) === null || _response4 === void 0 ? void 0 : (_response4$data = _response4.data) === null || _response4$data === void 0 ? void 0 : _response4$data.message : 'Có lỗi xảy ra.', [{
          text: 'Đồng ý'
        }]);

        // if (onError) {
        //   onError('');
        // }
      }
    } catch (error) {
      this.createConversationLoading = false;
      this.createConversationError = true;
      Alert.alert('Thông báo', 'Có lỗi xảy ra.', [{
        text: 'Đồng ý'
      }]);
      // if (onError) {
      //   onError(error);
      // }
      // Log(error);
    }
  }

  async createConversationWithReceiver(params, onSuccess, onError) {
    try {
      let response;
      if (appStore.appId === 'VTPost') {
        response = await services.create().vtpConversationWithReceiver(params);
      } else {
        response = await services.create().vtmConversationWithReceiver(params);
      }
      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 {
          var _response5, _response5$data, _response6, _response6$data;
          if (response.data.status === 1006) {
            Alert.alert('Thông báo', 'Số điện thoại của người dùng chưa có tài khoản tại ViettelPost.', [{
              text: 'Đồng ý'
            }]);
            return;
          }
          Alert.alert('Thông báo', (_response5 = response) !== null && _response5 !== void 0 && (_response5$data = _response5.data) !== null && _response5$data !== void 0 && _response5$data.message ? (_response6 = response) === null || _response6 === void 0 ? void 0 : (_response6$data = _response6.data) === null || _response6$data === void 0 ? void 0 : _response6$data.message : 'Có lỗi xảy ra.', [{
            text: 'Đồng ý'
          }]);
        }
      } else {
        var _response7, _response7$data, _response8, _response8$data;
        Alert.alert('Thông báo', (_response7 = response) !== null && _response7 !== void 0 && (_response7$data = _response7.data) !== null && _response7$data !== void 0 && _response7$data.message ? (_response8 = response) === null || _response8 === void 0 ? void 0 : (_response8$data = _response8.data) === null || _response8$data === void 0 ? void 0 : _response8$data.message : 'Có lỗi xảy ra.', [{
          text: 'Đồng ý'
        }]);
      }
    } catch (error) {
      Alert.alert('Thông báo', 'Có lỗi xảy ra.', [{
        text: 'Đồng ý'
      }]);
    }
  }
  async createConversationWithCS(params, onSuccess, onError) {
    try {
      let response;
      if (appStore.appId === 'VTPost') {
        response = await services.create().vtpConversationWithCS(params);
      } else {
        response = await services.create().vtmConversationWithCS(params);
      }
      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 {
            var _response9, _response9$data, _response10, _response10$data;
            Alert.alert('Thông báo', (_response9 = response) !== null && _response9 !== void 0 && (_response9$data = _response9.data) !== null && _response9$data !== void 0 && _response9$data.message ? (_response10 = response) === null || _response10 === void 0 ? void 0 : (_response10$data = _response10.data) === null || _response10$data === void 0 ? void 0 : _response10$data.message : 'Có lỗi xảy ra.', [{
              text: 'Đồng ý'
            }]);
          }
        } else {
          var _response11, _response11$data, _response12, _response12$data;
          Alert.alert('Thông báo', (_response11 = response) !== null && _response11 !== void 0 && (_response11$data = _response11.data) !== null && _response11$data !== void 0 && _response11$data.message ? (_response12 = response) === null || _response12 === void 0 ? void 0 : (_response12$data = _response12.data) === null || _response12$data === void 0 ? void 0 : _response12$data.message : 'Có lỗi xảy ra.', [{
            text: 'Đồng ý'
          }]);
        }
      } else {
        var _response13, _response13$data, _response14, _response14$data;
        Alert.alert('Thông báo', (_response13 = response) !== null && _response13 !== void 0 && (_response13$data = _response13.data) !== null && _response13$data !== void 0 && _response13$data.message ? (_response14 = response) === null || _response14 === void 0 ? void 0 : (_response14$data = _response14.data) === null || _response14$data === void 0 ? void 0 : _response14$data.message : 'Có lỗi xảy ra.', [{
          text: 'Đồng ý'
        }]);
      }
    } catch (error) {
      Alert.alert('Thông báo', 'Có lỗi xảy ra.', [{
        text: 'Đồng ý'
      }]);
    }
  }
  async onlineState() {
    try {
      const response = await services.create().onlineState({
        time: new Date().getTime()
      });
      Log(response);
    } catch (error) {}
  }
  async orderValidate(params, onSuccess, onError) {
    try {
      const response = await services.create().orderValidate(params);
      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.create().loginAdmin(params);
      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 {
        var _response$data4, _response$data5;
        Alert.alert('Thông báo', response !== null && response !== void 0 && (_response$data4 = response.data) !== null && _response$data4 !== void 0 && _response$data4.message ? response === null || response === void 0 ? void 0 : (_response$data5 = response.data) === null || _response$data5 === void 0 ? void 0 : _response$data5.message : 'Có lỗi xảy ra.', [{
          text: 'Đồng ý'
        }]);
      }
    } catch (error) {
      Alert.alert('Thông báo', 'Có lỗi xảy ra.', [{
        text: 'Đồng ý'
      }]);
    }
  }
  async loginVTP(params, onSuccess, onError) {
    try {
      const response = await services.create().loginVTP({
        ...params,
        ...{
          "clientId": "appvtp",
          "clientSecret": "vtp-appvtp"
        }
      });
      Log(response);
      if (response.status === 200) {
        const responseClient = await services.create().loginVTPClient({
          "Source": 2,
          "Key": "tw498twe89th238Ar5y98erfwsbvsagsdfepiafklE98rbhq38r543890314123rusdf87bq38972438r4378461237eryqwrf9b",
          "TokenSSO": response.data.accessToken,
          "refreshToken": "0f97f1f484f4dfa7fb4084340cdf643ec29cc5e80bc162049fe1cf267fe26934",
          "TokenIdSSO": "",
          "Type": "VTP"
        });
        Log(responseClient);
        if (responseClient.status === 200) {
          responseClient.data.data.TokenSSO = response.data.accessToken;
          onSuccess(responseClient.data.data);
        } else {
          alert(responseClient.data.errorMessage);
        }
      } else {
        var _response$data6, _response$data7;
        Alert.alert('Thông báo', response !== null && response !== void 0 && (_response$data6 = response.data) !== null && _response$data6 !== void 0 && _response$data6.message ? response === null || response === void 0 ? void 0 : (_response$data7 = response.data) === null || _response$data7 === void 0 ? void 0 : _response$data7.message : 'Có lỗi xảy ra.', [{
          text: 'Đồng ý'
        }]);
      }
    } catch (error) {
      alert(error);
    }
  }
  async loginVTM(params, onSuccess, onError) {
    try {
      const response = await services.create().loginVTM({
        ...params,
        ...{
          "source": -10,
          "latitude": 0,
          "longtitude": 0,
          "DeviceID": "string",
          "uniqueID": "string"
        }
      });
      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.create().getConversationDetail(params);
      Log(response);
      if (response.status === 200) {
        onSuccess(response.data.data);
      } else {
        var _response$data8, _response$data9;
        Alert.alert('Thông báo', response !== null && response !== void 0 && (_response$data8 = response.data) !== null && _response$data8 !== void 0 && _response$data8.message ? response === null || response === void 0 ? void 0 : (_response$data9 = response.data) === null || _response$data9 === void 0 ? void 0 : _response$data9.message : 'Có lỗi xảy ra.', [{
          text: 'Đồng ý'
        }]);
      }
    } catch (error) {
      Alert.alert('Thông báo', 'Có lỗi xảy ra.', [{
        text: 'Đồng ý'
      }]);
    }
  }
  async vtpWithCS(params, onSuccess, onError) {
    try {
      const response = await services.create().vtpConversationWithCS(params);
      Log(response);
      if (response.status === 200 || response.status === 201) {
        onSuccess(response.data.data);
      } else {
        var _response$data10, _response$data11;
        Alert.alert('Thông báo', response !== null && response !== void 0 && (_response$data10 = response.data) !== null && _response$data10 !== void 0 && _response$data10.message ? response === null || response === void 0 ? void 0 : (_response$data11 = response.data) === null || _response$data11 === void 0 ? void 0 : _response$data11.message : 'Có lỗi xảy ra.', [{
          text: 'Đồng ý'
        }]);
      }
    } catch (error) {
      Alert.alert('Thông báo', 'Có lỗi xảy ra.', [{
        text: 'Đồng ý'
      }]);
    }
  }
  async vtmWithCS(params, onSuccess, onError) {
    try {
      const response = await services.create().vtmConversationWithCS(params);
      Log(response);
      if (response.status === 200 || response.status === 201) {
        onSuccess(response.data.data);
      } else {
        var _response$data12, _response$data13;
        Alert.alert('Thông báo', response !== null && response !== void 0 && (_response$data12 = response.data) !== null && _response$data12 !== void 0 && _response$data12.message ? response === null || response === void 0 ? void 0 : (_response$data13 = response.data) === null || _response$data13 === void 0 ? void 0 : _response$data13.message : 'Có lỗi xảy ra.', [{
          text: 'Đồng ý'
        }]);
      }
    } catch (error) {
      Alert.alert('Thông báo', 'Có lỗi xảy ra.', [{
        text: 'Đồng ý'
      }]);
    }
  }
}
const appStore = new AppStore();
export default appStore;
//# sourceMappingURL=AppStore.js.map