"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _StyledComponents = require("../styled/StyledComponents");
var _SendInput = _interopRequireDefault(require("../styled/SendInput"));
var _reactRedux = require("react-redux");
var _xmppProvider = require("../../context/xmppProvider");
var _MessageList = _interopRequireDefault(require("../MainComponents/MessageList"));
var _ModalHeaderComponent = _interopRequireDefault(require("../Modals/ModalHeaderComponent"));
var _roomsSlice = require("../../roomStore/roomsSlice");
var _EditWrapper = require("../MainComponents/EditWrapper");
var _useSendMessage = require("../../hooks/useSendMessage");
var _createMainMessageForThread = require("../../helpers/createMainMessageForThread");
var _useRoomState = require("../../hooks/useRoomState");
var _useChatSettingState = require("../../hooks/useChatSettingState");
var _reactNative = require("react-native");
var _CustomTypingIndicator = _interopRequireDefault(require("../styled/StyledInputComponents/CustomTypingIndicator"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const ThreadWrapper = ({
  activeMessage,
  user,
  customMessageComponent: CustomMessageComponent
}) => {
  const {
    client
  } = (0, _xmppProvider.useXmppClient)();
  const dispatch = (0, _reactRedux.useDispatch)();
  const {
    loading,
    roomsList,
    editAction,
    activeRoomJID
  } = (0, _useRoomState.useRoomState)();
  const {
    config
  } = (0, _useChatSettingState.useChatSettingState)();
  const {
    sendMessage: sendMs,
    sendMedia: sendMessageMedia,
    sendEditMessage,
    isLastMessageFromUserAndProcessing
  } = (0, _useSendMessage.useSendMessage)();
  const [isLoadingMore, setIsLoadingMore] = (0, _react.useState)(false);
  const [isChecked, setIsChecked] = (0, _react.useState)(false);
  const slideAnim = (0, _react.useRef)(new _reactNative.Animated.Value(300)).current;
  const panResponder = (0, _react.useRef)(_reactNative.PanResponder.create({
    onStartShouldSetPanResponder: (e, gestureState) => {
      return gestureState.x0 <= 100;
    },
    onMoveShouldSetPanResponder: (e, gestureState) => {
      return gestureState.x0 <= 100 && gestureState.dx > 0;
    },
    onPanResponderMove: (e, gestureState) => {
      if (gestureState.x0 <= 100 && gestureState.dx > 0) {
        slideAnim.setValue(gestureState.dx);
      }
    },
    onPanResponderRelease: (e, gestureState) => {
      if (gestureState.x0 <= 100 && gestureState.dx > 150) {
        closeThread();
      } else {
        _reactNative.Animated.spring(slideAnim, {
          toValue: 0,
          useNativeDriver: true
        }).start();
      }
    }
  })).current;
  const loadMoreMessages = (0, _react.useCallback)(async (chatJID, max, idOfMessageBefore) => {
    if (!isLoadingMore) {
      client?.getHistoryStanza(chatJID, max, idOfMessageBefore).then(() => {
        setIsLoadingMore(false);
      }).catch(err => {
        console.warn('getHistoryStanza failed', err);
        setIsLoadingMore(false);
      });
    }
  }, [client]);
  const sendMessage = (0, _react.useCallback)(message => {
    sendMs(message, activeMessage.roomJid, true, isChecked, (0, _createMainMessageForThread.createMainMessageForThread)(activeMessage));
  }, [activeMessage, isChecked]);
  const sendMedia = (0, _react.useCallback)((data, type) => {
    sendMessageMedia(data, type, activeMessage.roomJid, true, isChecked, (0, _createMainMessageForThread.createMainMessageForThread)(activeMessage));
  }, [activeMessage, isChecked]);
  const sendStartComposing = (0, _react.useCallback)(() => {
    if (config?.disableTypingIndicator) {
      return;
    }
    client?.sendTypingRequestStanza(activeMessage.roomJid, `${user.firstName} ${user.lastName}`, true);
  }, []);
  const sendEndComposing = (0, _react.useCallback)(() => {
    if (config?.disableTypingIndicator) {
      return;
    }
    client?.sendTypingRequestStanza(activeMessage.roomJid, `${user.firstName} ${user.lastName}`, false);
  }, []);
  const onCloseEdit = () => {
    dispatch((0, _roomsSlice.setEditAction)({
      isEdit: false
    }));
  };
  const closeThread = () => {
    dispatch((0, _roomsSlice.setCloseActiveMessage)({
      chatJID: activeMessage.roomJid
    }));
    dispatch((0, _roomsSlice.setEditAction)({
      isEdit: false
    }));
  };
  (0, _react.useEffect)(() => {
    if (activeMessage?.activeMessage) {
      _reactNative.Animated.timing(slideAnim, {
        toValue: 0,
        duration: 300,
        useNativeDriver: true
      }).start();
    } else {
      _reactNative.Animated.timing(slideAnim, {
        toValue: 300,
        duration: 300,
        useNativeDriver: true
      }).start();
    }
  }, [activeMessage?.activeMessage]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.Animated.View, {
    ...panResponder.panHandlers,
    style: [styles.threadContainer, {
      transform: [{
        translateX: slideAnim
      }]
    }
    // ...config?.chatRoomStyles,
    ],
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_ModalHeaderComponent.default, {
      headerTitle: "Thread",
      handleCloseModal: closeThread
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_MessageList.default, {
      loadMoreMessages: loadMoreMessages,
      CustomMessage: CustomMessageComponent,
      user: user,
      roomJID: activeMessage.roomJid,
      config: config,
      loading: isLoadingMore,
      activeMessage: activeMessage,
      isReply: true
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_StyledComponents.AlsoContainer, {
      onPress: () => setIsChecked(prev => !prev),
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_StyledComponents.AlsoCheckbox, {
        accentColor: isChecked ? config?.colors?.primary || '#0052CD' : '#fff'
        // checked={isChecked}
        // onPress={() => setIsChecked(!isChecked)}
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
        children: "Also send to"
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableOpacity, {
        onPress: closeThread,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
          style: {
            color: config?.colors?.primary || '#0052CD',
            fontWeight: 500
          },
          children: roomsList[activeMessage.roomJid].name
        })
      })]
    }), editAction && editAction.isEdit && /*#__PURE__*/(0, _jsxRuntime.jsx)(_EditWrapper.EditWrapper, {
      text: editAction.text || '',
      onClose: onCloseEdit
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_SendInput.default, {
      editMessage: editAction && editAction.text,
      sendMedia: sendMedia,
      sendMessage: editAction && editAction.isEdit ? sendEditMessage : sendMessage,
      config: config,
      onFocus: sendStartComposing,
      onBlur: sendEndComposing,
      isLoading: loading,
      isMessageProcessing: isLastMessageFromUserAndProcessing(activeMessage.roomJid)
    }), config?.customTypingIndicator?.enabled && (config.customTypingIndicator.position === 'overlay' || config.customTypingIndicator.position === 'floating') && roomsList[activeMessage.roomJid]?.composing && /*#__PURE__*/(0, _jsxRuntime.jsx)(_CustomTypingIndicator.default, {
      usersTyping: roomsList[activeMessage.roomJid]?.composingList || ['User'],
      text: config.customTypingIndicator.text,
      position: config.customTypingIndicator.position,
      styles: config.customTypingIndicator.styles,
      customComponent: config.customTypingIndicator.customComponent,
      isVisible: roomsList[activeMessage.roomJid]?.composing || false
    })]
  });
};
var _default = exports.default = ThreadWrapper;
const styles = _reactNative.StyleSheet.create({
  threadContainer: {
    zIndex: 999,
    position: 'absolute',
    top: 0,
    bottom: 0,
    right: 0,
    width: '100%',
    height: '100%',
    backgroundColor: '#f3f6fc',
    flexDirection: 'column',
    justifyContent: 'space-between',
    flex: 1
  },
  text: {
    color: '#fff',
    fontSize: 18
  }
});
//# sourceMappingURL=ThreadWrapper.js.map