"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.chatVT = exports.ListChat = exports.BottomSheetChatVTM = exports.BottomSheetChat = void 0;
var _reactNativeNavigation = require("react-native-navigation");
var _AppStore = _interopRequireDefault(require("./screens/AppStore"));
var _listchat = require("./screens/listchat");
var _chat = require("./screens/chat");
var _reactNativeGestureHandler = require("react-native-gesture-handler");
var MyAsyncStorage = _interopRequireWildcard(require("./utils/MyAsyncStorage"));
var _socket = _interopRequireDefault(require("./socket"));
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
var _Styles = _interopRequireDefault(require("./Styles"));
var _react = _interopRequireDefault(require("react"));
var _reactNative = require("react-native");
var _attachs = require("./screens/attachs");
var _webview = require("./screens/webview");
var _bottomSheetChatOptions = _interopRequireDefault(require("./components/bottomSheetChatOptions"));
var _VTM = _interopRequireDefault(require("./components/bottomSheetChatOptions/VTM"));
var _ListChatStore = _interopRequireDefault(require("./screens/listchat/ListChatStore"));
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); } // import {ChatStack} from './App.js'
function safeAreaProviderHOC(Component) {
  function Wrapper(props) {
    return /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaProvider, {
      style: [{
        flex: 1
      }]
    }, /*#__PURE__*/_react.default.createElement(Component, props));
  }
  Wrapper.displayName = `safeAreaProviderHOC(${Component.displayName || Component.name})`;

  // @ts-ignore - hoistNonReactStatics uses old version of @types/react
  (0, _hoistNonReactStatics.default)(Wrapper, Component);
  return Wrapper;
}
class ChatVT {
  constructor() {
    _defineProperty(this, "AsyncStorage", void 0);
    _defineProperty(this, "interval", void 0);
    _defineProperty(this, "toChatWithCustomer", async (componentId, order_code, type) => {
      _AppStore.default.createConversation({
        order_number: order_code,
        chat_type: type
      }, conversation => {
        //Navigation.pop('ChatScreen')
        _reactNativeNavigation.Navigation.push(componentId, {
          component: {
            id: 'ChatScreen',
            name: 'ChatScreen',
            options: {
              popGesture: false,
              bottomTabs: {
                visible: false
              },
              topBar: {
                visible: false,
                height: 0
              }
            },
            passProps: {
              data: conversation,
              order: order
            }
          }
        });
      }, error => alert(error));
    });
  }
  registerScreen() {
    _reactNativeNavigation.Navigation.registerComponent('ListChatScreen', () => (0, _reactNativeGestureHandler.gestureHandlerRootHOC)(safeAreaProviderHOC(_listchat.ListChatScreen)));
    _reactNativeNavigation.Navigation.registerComponent('ChatScreen', () => (0, _reactNativeGestureHandler.gestureHandlerRootHOC)(safeAreaProviderHOC(_chat.ChatScreen)));
    _reactNativeNavigation.Navigation.registerComponent('AttachsScreen', () => (0, _reactNativeGestureHandler.gestureHandlerRootHOC)(safeAreaProviderHOC(_attachs.AttachsScreen)));
    _reactNativeNavigation.Navigation.registerComponent('ViewFileScreen', () => (0, _reactNativeGestureHandler.gestureHandlerRootHOC)(safeAreaProviderHOC(_webview.ViewFileScreen)));
    const screenEventListener = _reactNativeNavigation.Navigation.events().registerComponentDidAppearListener(_ref => {
      var _listChatStore$data;
      let {
        componentId,
        componentName,
        passProps
      } = _ref;
      if (componentName === 'navigation.VTMan.MessengerScreen.TK' && ((_listChatStore$data = _ListChatStore.default.data) === null || _listChatStore$data === void 0 ? void 0 : _listChatStore$data.length) === 0) {
        _ListChatStore.default.page = 0;
        _ListChatStore.default.getData({});
      }
    });
    // screenEventListener.remove();
  }

  /** */
  init(env, storage, lang, appId, token, tokenSSO, onSuccess, onError) {
    this.registerScreen();
    this.AsyncStorage = storage;
    _AppStore.default.appId = appId;
    _AppStore.default.env = env;
    _AppStore.default.changeLanguage(lang);
    _AppStore.default.Auth({
      token: token,
      token_sso: tokenSSO
    }, res => {
      var _listChatStore$data2;
      _ListChatStore.default.data = [];
      _ListChatStore.default.dataPin = [];
      // if(this.interval){
      //   clearInterval(this.interval)
      // }
      // this.interval = setInterval(()=>{
      //   appStore.onlineState()
      // }, 30000)
      if (((_listChatStore$data2 = _ListChatStore.default.data) === null || _listChatStore$data2 === void 0 ? void 0 : _listChatStore$data2.length) === 0) {
        _ListChatStore.default.page = 0;
        _ListChatStore.default.getData({});
      }
      if (onSuccess) onSuccess(res);
    }, onError);
  }
  toListChat(componentId) {
    if (componentId) {
      _AppStore.default.componentId = componentId;
    }
    _reactNativeNavigation.Navigation.push(componentId, {
      component: {
        id: 'ListChatScreen',
        name: 'ListChatScreen',
        options: {
          popGesture: false,
          bottomTabs: {
            visible: false
          },
          topBar: {
            visible: false,
            height: 0
          }
        }
      }
    });
  }
  toChat(componentId, orderChat, is_receiver) {
    if (componentId) {
      _AppStore.default.componentId = componentId;
    }
    if (orderChat) {
      _AppStore.default.createConversation({
        vtm_user_ids: orderChat.vtm_user_ids,
        order_number: orderChat.order_number,
        vtp_phone_numbers: orderChat.vtp_phone_numbers,
        is_receiver: is_receiver
      }, conversation => {
        //Navigation.pop('ChatScreen')
        _reactNativeNavigation.Navigation.push(componentId, {
          component: {
            id: 'ChatScreen',
            name: 'ChatScreen',
            options: {
              popGesture: false,
              bottomTabs: {
                visible: false
              },
              topBar: {
                visible: false,
                height: 0
              }
            },
            passProps: {
              data: conversation,
              order: orderChat === null || orderChat === void 0 ? void 0 : orderChat.order
            }
          }
        });
      }, error => alert(error));
    }
  }
  chatWithReceiver(componentId, order, is_receiver) {
    if (componentId) {
      _AppStore.default.componentId = componentId;
    }
    if (order !== null && order !== void 0 && order.ORDER_NUMBER) {
      _AppStore.default.createConversationWithReceiver({
        order_number: order === null || order === void 0 ? void 0 : order.ORDER_NUMBER,
        is_receiver: is_receiver
      }, conversation => {
        //Navigation.pop('ChatScreen')
        _reactNativeNavigation.Navigation.push(componentId, {
          component: {
            id: 'ChatScreen',
            name: 'ChatScreen',
            options: {
              popGesture: false,
              bottomTabs: {
                visible: false
              },
              topBar: {
                visible: false,
                height: 0
              }
            },
            passProps: {
              data: conversation,
              order: order
            }
          }
        });
      }, error => alert(error));
    }
  }
  chatWithCS(componentId, order) {
    if (componentId) {
      _AppStore.default.componentId = componentId;
    }
    _AppStore.default.createConversationWithCS({
      order_number: order === null || order === void 0 ? void 0 : order.ORDER_NUMBER
    }, conversation => {
      //Navigation.pop('ChatScreen')
      _reactNativeNavigation.Navigation.push(componentId, {
        component: {
          id: 'ChatScreen',
          name: 'ChatScreen',
          options: {
            popGesture: false,
            bottomTabs: {
              visible: false
            },
            topBar: {
              visible: false,
              height: 0
            }
          },
          passProps: {
            data: conversation
          }
        }
      });
    }, error => alert(error));
  }
  handleNotification(env, storage, lang, appId, token, tokenSSO, onSuccess, onError, componentId, conversation_id) {
    if (!_AppStore.default.user.token) {
      this.init(env, storage, lang, appId, token, tokenSSO, () => {
        _AppStore.default.conversationDetail({
          conversation_id: conversation_id
        }, conversation => {
          try {
            _reactNativeNavigation.Navigation.popTo('ListChatScreen');
          } catch (e) {}
          setTimeout(() => {
            try {
              _reactNativeNavigation.Navigation.push(componentId ? componentId : 'ListChatScreen', {
                component: {
                  id: 'ChatScreen',
                  name: 'ChatScreen',
                  options: {
                    popGesture: false,
                    bottomTabs: {
                      visible: false
                    },
                    topBar: {
                      visible: false,
                      height: 0
                    }
                  },
                  passProps: {
                    data: conversation
                  }
                }
              });
              console.log('ChatScreen end');
            } catch (e) {
              console.log(e);
            }
          }, 200);
        });
      }, onError);
    } else {
      _AppStore.default.conversationDetail({
        conversation_id: conversation_id
      }, async conversation => {
        try {
          _reactNativeNavigation.Navigation.popTo('ListChatScreen');
        } catch (e) {}
        setTimeout(() => {
          _reactNativeNavigation.Navigation.push(componentId ? componentId : 'ListChatScreen', {
            component: {
              id: 'ChatScreen',
              name: 'ChatScreen',
              options: {
                popGesture: false,
                bottomTabs: {
                  visible: false
                },
                topBar: {
                  visible: false,
                  height: 0
                }
              },
              passProps: {
                data: conversation
              }
            }
          });
        }, 200);
      });
    }
  }

  /** Lang: 'VN' | 'EN' */
  changeLanguage(lang) {
    _AppStore.default.changeLanguage(lang);
  }
  loginAdmin(componentId, storage, username, password, onSuccess, onError) {
    if (componentId) {
      _AppStore.default.componentId = componentId;
    }
    this.registerScreen();
    _AppStore.default.loginAdmin({
      username,
      password
    }, async data => {
      this.AsyncStorage = storage;
      _AppStore.default.appId = 'Admin';
      _AppStore.default.env = 'DEV';
      _AppStore.default.changeLanguage('VI');
      await MyAsyncStorage.save(MyAsyncStorage.USER, data);
      await _socket.default.getInstance().init();
      _reactNativeNavigation.Navigation.push(componentId, {
        component: {
          id: 'ListChatScreen',
          name: 'ListChatScreen',
          options: {
            popGesture: false,
            bottomTabs: {
              visible: false
            },
            topBar: {
              visible: false,
              height: 0
            }
          }
        }
      });
    });
  }
  loginVTP(componentId, storage, username, password, onSuccess, onError) {
    _reactNativeNavigation.Navigation.registerComponent('ListChatScreen', () => (0, _reactNativeGestureHandler.gestureHandlerRootHOC)(_listchat.ListChatScreen));
    _reactNativeNavigation.Navigation.registerComponent('ChatScreen', () => (0, _reactNativeGestureHandler.gestureHandlerRootHOC)(_chat.ChatScreen));
    _AppStore.default.loginVTP({
      username,
      password
    }, async data => {
      this.init('DEV', storage, 'VN', 'VTPost', data.TokenKey, data.TokenSSO, onSuccess);
    });
  }
  loginVTM(componentId, storage, username, password, onSuccess, onError) {
    this.registerScreen();
    _AppStore.default.loginVTM({
      phone: username,
      ma_buucuc: password
    }, async data => {
      this.init('DEV', storage, 'VN', 'VTMan', data.token, '', onSuccess);
    });
  }
  vtpWithCS(componentId, order_number) {
    _AppStore.default.vtpWithCS({
      order_number
    }, async data => {
      //Navigation.pop('ChatScreen')
      _reactNativeNavigation.Navigation.push(componentId, {
        component: {
          id: 'ChatScreen',
          name: 'ChatScreen',
          options: {
            popGesture: false,
            bottomTabs: {
              visible: false
            },
            topBar: {
              visible: false,
              height: 0
            }
          },
          passProps: {
            data: data
          }
        }
      });
    });
  }
  vtmWithCS(componentId, order_number) {
    _AppStore.default.vtmWithCS({
      order_number
    }, async data => {
      //Navigation.pop('ChatScreen')
      _reactNativeNavigation.Navigation.push(componentId, {
        component: {
          id: 'ChatScreen',
          name: 'ChatScreen',
          options: {
            popGesture: false,
            bottomTabs: {
              visible: false
            },
            topBar: {
              visible: false,
              height: 0
            }
          },
          passProps: {
            data: data
          }
        }
      });
    });
  }
}
const chatVT = new ChatVT();
exports.chatVT = chatVT;
const ListChat = _listchat.ListChatScreen;
exports.ListChat = ListChat;
const BottomSheetChat = _bottomSheetChatOptions.default;
exports.BottomSheetChat = BottomSheetChat;
const BottomSheetChatVTM = _VTM.default;
exports.BottomSheetChatVTM = BottomSheetChatVTM;
//# sourceMappingURL=index.js.map