import React, { useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
import { Image, SafeAreaView, TouchableNativeFeedback, TouchableOpacity, View } from 'react-native';
import { BottomSheetFlatList, BottomSheetModal, BottomSheetModalProvider } from '../bottomSheet/bottom-sheet';
import chatStore from '../../screens/chat/ChatStore';
import { MText as Text } from '../index';
import quickMessageStore from '../../screens/chat/QuickMessageStore';
import colors from '../../Styles';
import listChatStore from '../../screens/listchat/ListChatStore';
import appStore from '../../screens/AppStore';
import { formatTimeLastMessage } from '../../utils';
import { chatVT } from '../../index';
import { Navigation } from 'react-native-navigation';
import services from '../../services';
const BottomSheetChatOptions = /*#__PURE__*/React.forwardRef((props, ref) => {
  const snapPoints = useMemo(() => ['30%'], []);
  const bottomSheetRef = useRef();
  const [data, setData] = useState([]);
  const [order, setOrder] = useState({});
  useImperativeHandle(ref,
  // forwarded ref
  () => {
    return {
      updateData(data, order) {
        setData(data);
        setOrder(order);
        console.log('update data', data, order);
      },
      present() {
        var _bottomSheetRef$curre;
        (_bottomSheetRef$curre = bottomSheetRef.current) === null || _bottomSheetRef$curre === void 0 ? void 0 : _bottomSheetRef$curre.present();
      },
      dismiss() {
        var _bottomSheetRef$curre2;
        (_bottomSheetRef$curre2 = bottomSheetRef.current) === null || _bottomSheetRef$curre2 === void 0 ? void 0 : _bottomSheetRef$curre2.dismiss();
      }
    }; // the forwarded ref value
  }, []);
  const toChat = listUserId => {
    appStore.createConversation({
      vtm_user_ids: listUserId,
      order_number: order.ORDER_NUMBER
    }, conversation => {
      Navigation.push(props.componentId, {
        component: {
          name: 'ChatScreen',
          options: {
            popGesture: false,
            bottomTabs: {
              visible: false
            },
            topBar: {
              visible: false,
              height: 0
            }
          },
          passProps: {
            data: conversation,
            order: order
          }
        }
      });
    }, error => alert(error));
  };
  const toChatGroup = () => {
    try {
      var _ref$current;
      ref === null || ref === void 0 ? void 0 : (_ref$current = ref.current) === null || _ref$current === void 0 ? void 0 : _ref$current.dismiss();
    } catch (e) {}
  };
  const getUserIdFromOrder = async order_code => {
    try {
      var _response$data;
      const response = await services.create().getVTMCustomerByOrder({
        order_code
      });
      console.log(response);
      if ((_response$data = response.data) !== null && _response$data !== void 0 && _response$data.data) {
        var _response$data2, _response$data2$data;
        return (_response$data2 = response.data) === null || _response$data2 === void 0 ? void 0 : (_response$data2$data = _response$data2.data) === null || _response$data2$data === void 0 ? void 0 : _response$data2$data.cus_id;
      } else {
        return 0;
      }
    } catch (e) {
      console.log(e);
      return -1;
    }
  };
  const toChatWithSender = async order_code => {
    try {
      var _ref$current2;
      ref === null || ref === void 0 ? void 0 : (_ref$current2 = ref.current) === null || _ref$current2 === void 0 ? void 0 : _ref$current2.dismiss();
    } catch (e) {}
    appStore.createConversation({
      vtp_user_ids: [await getUserIdFromOrder(order_code)],
      order_number: order.ma_phieugui
    }, conversation => {
      Navigation.push(props.componentId, {
        component: {
          name: 'ChatScreen',
          options: {
            popGesture: false,
            bottomTabs: {
              visible: false
            },
            topBar: {
              visible: false,
              height: 0
            }
          },
          passProps: {
            data: conversation,
            order: order
          }
        }
      });
    }, error => alert(error));
  };
  const toChatReceiver = () => {
    try {
      var _ref$current3;
      ref === null || ref === void 0 ? void 0 : (_ref$current3 = ref.current) === null || _ref$current3 === void 0 ? void 0 : _ref$current3.dismiss();
    } catch (e) {}
    appStore.createConversationWithReceiver({
      order_number: order.ORDER_NUMBER
    }, conversation => {
      Navigation.push(props.componentId, {
        component: {
          name: 'ChatScreen',
          options: {
            popGesture: false,
            bottomTabs: {
              visible: false
            },
            topBar: {
              visible: false,
              height: 0
            }
          },
          passProps: {
            data: conversation,
            order
          }
        }
      });
    }, error => alert(error));
  };
  const renderItem = (item, index) => {
    return /*#__PURE__*/React.createElement(TouchableOpacity, {
      onPress: () => {
        try {
          var _ref$current4;
          ref === null || ref === void 0 ? void 0 : (_ref$current4 = ref.current) === null || _ref$current4 === void 0 ? void 0 : _ref$current4.dismiss();
        } catch (e) {}
        toChat([item.USERID]);
      },
      style: {
        flexDirection: 'row',
        backgroundColor: 'white',
        paddingVertical: 12,
        paddingHorizontal: 16
      }
    }, /*#__PURE__*/React.createElement(View, {
      style: {
        height: 48,
        width: 48,
        resizeMode: 'center',
        marginRight: 12
      }
    }, /*#__PURE__*/React.createElement(Image, {
      style: {
        height: 48,
        width: 48,
        resizeMode: 'center'
      },
      source: require('../../assets/avatar_default.png')
    })), /*#__PURE__*/React.createElement(View, {
      style: {
        flex: 1
      }
    }, /*#__PURE__*/React.createElement(Text, {
      style: {
        fontSize: 17,
        fontWeight: '600',
        color: colors.primaryText
      }
    }, `Chat với bưu tá ${item.RECEIVER_POSTMAN === 1 ? 'lấy' : 'giao'}`), /*#__PURE__*/React.createElement(Text, {
      style: {
        fontSize: 15,
        fontWeight: '500',
        color: colors.neutralText,
        paddingTop: 4
      }
    }, item.FIRSTNAME + ' ' + item.LASTNAME)));
  };
  return /*#__PURE__*/React.createElement(BottomSheetModalProvider, null, /*#__PURE__*/React.createElement(BottomSheetModal, {
    ref: bottomSheetRef,
    backdropComponent: () => {
      return /*#__PURE__*/React.createElement(TouchableOpacity, {
        activeOpacity: 1,
        onPress: () => {
          var _ref$current5;
          return ref === null || ref === void 0 ? void 0 : (_ref$current5 = ref.current) === null || _ref$current5 === void 0 ? void 0 : _ref$current5.dismiss();
        },
        style: {
          position: 'absolute',
          width: '100%',
          height: '100%',
          backgroundColor: '#00000059'
        }
      });
    },
    index: 0,
    snapPoints: snapPoints,
    onDismiss: () => {}
  }, appStore.appId === 'VTPost' && /*#__PURE__*/React.createElement(SafeAreaView, {
    style: {
      flex: 1
    }
  }, /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: toChatReceiver,
    style: {
      flexDirection: 'row',
      backgroundColor: 'white',
      paddingVertical: 12,
      paddingHorizontal: 16
    }
  }, /*#__PURE__*/React.createElement(View, {
    style: {
      height: 48,
      width: 48,
      resizeMode: 'center',
      marginRight: 12
    }
  }, /*#__PURE__*/React.createElement(Image, {
    style: {
      height: 48,
      width: 48,
      resizeMode: 'center'
    },
    source: require('../../assets/avatar_default.png')
  })), /*#__PURE__*/React.createElement(View, {
    style: {
      flex: 1
    }
  }, /*#__PURE__*/React.createElement(Text, {
    style: {
      fontSize: 17,
      fontWeight: '600',
      color: colors.primaryText
    }
  }, `Chat với người nhận`), /*#__PURE__*/React.createElement(Text, {
    style: {
      fontSize: 15,
      fontWeight: '500',
      color: colors.neutralText,
      paddingTop: 4
    }
  }, order.RECEIVER_FULLNAME))), data.length > 0 && /*#__PURE__*/React.createElement(View, {
    style: {
      backgroundColor: 'white',
      height: 1
    }
  }, /*#__PURE__*/React.createElement(View, {
    style: {
      backgroundColor: '#E5E5E5',
      height: 1,
      marginLeft: 76,
      marginRight: 16
    }
  })), /*#__PURE__*/React.createElement(BottomSheetFlatList, {
    data: data,
    renderItem: _ref => {
      let {
        item,
        index
      } = _ref;
      return renderItem(item, index);
    },
    ItemSeparatorComponent: () => /*#__PURE__*/React.createElement(View, {
      style: {
        backgroundColor: 'white',
        height: 1
      }
    }, /*#__PURE__*/React.createElement(View, {
      style: {
        backgroundColor: '#E5E5E5',
        height: 1,
        marginLeft: 76,
        marginRight: 16
      }
    }))
  })), appStore.appId === 'VTMan' && /*#__PURE__*/React.createElement(SafeAreaView, {
    style: {
      flex: 1
    }
  }, /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: toChatGroup,
    style: {
      flexDirection: 'row',
      backgroundColor: 'white',
      paddingVertical: 12,
      paddingHorizontal: 16
    }
  }, /*#__PURE__*/React.createElement(View, {
    style: {
      height: 48,
      width: 48,
      resizeMode: 'center',
      marginRight: 12
    }
  }, /*#__PURE__*/React.createElement(Image, {
    style: {
      height: 48,
      width: 48,
      resizeMode: 'center'
    },
    source: require('../../assets/avatar_create_group.png')
  })), /*#__PURE__*/React.createElement(View, {
    style: {
      flex: 1
    }
  }, /*#__PURE__*/React.createElement(Text, {
    style: {
      fontSize: 17,
      fontWeight: '600',
      color: colors.primaryText
    }
  }, `Chat nhóm`), /*#__PURE__*/React.createElement(Text, {
    style: {
      fontSize: 15,
      fontWeight: '500',
      color: colors.neutralText,
      paddingTop: 4
    }
  }, 'Tham gia của nhiều người'))), /*#__PURE__*/React.createElement(View, {
    style: {
      backgroundColor: 'white',
      height: 1
    }
  }, /*#__PURE__*/React.createElement(View, {
    style: {
      backgroundColor: '#E5E5E5',
      height: 1,
      marginLeft: 76,
      marginRight: 16
    }
  })), /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: () => {
      toChatWithSender(order.ma_phieugui);
    },
    style: {
      flexDirection: 'row',
      backgroundColor: 'white',
      paddingVertical: 12,
      paddingHorizontal: 16
    }
  }, /*#__PURE__*/React.createElement(View, {
    style: {
      height: 48,
      width: 48,
      resizeMode: 'center',
      marginRight: 12
    }
  }, /*#__PURE__*/React.createElement(Image, {
    style: {
      height: 48,
      width: 48,
      resizeMode: 'center'
    },
    source: require('../../assets/avatar_default.png')
  })), /*#__PURE__*/React.createElement(View, {
    style: {
      flex: 1
    }
  }, /*#__PURE__*/React.createElement(Text, {
    style: {
      fontSize: 17,
      fontWeight: '600',
      color: colors.primaryText
    }
  }, `Chat với khách hàng gửi`), /*#__PURE__*/React.createElement(Text, {
    style: {
      fontSize: 15,
      fontWeight: '500',
      color: colors.neutralText,
      paddingTop: 4
    }
  }, order.ten_khgui))), /*#__PURE__*/React.createElement(View, {
    style: {
      backgroundColor: 'white',
      height: 1
    }
  }, /*#__PURE__*/React.createElement(View, {
    style: {
      backgroundColor: '#E5E5E5',
      height: 1,
      marginLeft: 76,
      marginRight: 16
    }
  })), /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: () => {
      try {
        var _ref$current6;
        ref === null || ref === void 0 ? void 0 : (_ref$current6 = ref.current) === null || _ref$current6 === void 0 ? void 0 : _ref$current6.dismiss();
      } catch (e) {}
      // toChatWithSender(order.tel_khnhan)
    },

    style: {
      flexDirection: 'row',
      backgroundColor: 'white',
      paddingVertical: 12,
      paddingHorizontal: 16
    }
  }, /*#__PURE__*/React.createElement(View, {
    style: {
      height: 48,
      width: 48,
      resizeMode: 'center',
      marginRight: 12
    }
  }, /*#__PURE__*/React.createElement(Image, {
    style: {
      height: 48,
      width: 48,
      resizeMode: 'center'
    },
    source: require('../../assets/avatar_default.png')
  })), /*#__PURE__*/React.createElement(View, {
    style: {
      flex: 1
    }
  }, /*#__PURE__*/React.createElement(Text, {
    style: {
      fontSize: 17,
      fontWeight: '600',
      color: colors.primaryText
    }
  }, `Chat với khách hàng nhận`), /*#__PURE__*/React.createElement(Text, {
    style: {
      fontSize: 15,
      fontWeight: '500',
      color: colors.neutralText,
      paddingTop: 4
    }
  }, order.ten_khnhan))))));
});
export default BottomSheetChatOptions;
//# sourceMappingURL=index.js.map