"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = GlobalFeed;
var _react = require("react");
var _reactNative = require("react-native");
var _feed = require("../../../core/legacy/feed");
var _useAuth = _interopRequireDefault(require("../../../core/hooks/useAuth"));
var _PostList = _interopRequireDefault(require("../../components/legacy/Social/PostList"));
var _styles = require("./styles");
var _post = require("../../../core/utils/post");
var _globalfeedSlice = _interopRequireDefault(require("../../../core/stores/slices/globalfeedSlice"));
var _store = require("../../../core/stores/store");
var _native = require("@react-navigation/native");
var _MyStories = _interopRequireDefault(require("../../components/legacy/MyStories"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// import { useTranslation } from 'react-i18next';

function GlobalFeed() {
  const {
    postList
  } = (0, _store.useUIKitSelector)(state => state.globalFeed);
  const [refreshing, setRefreshing] = (0, _react.useState)(false);
  const {
    deleteByPostId,
    setNewGlobalFeed
  } = _globalfeedSlice.default.actions;
  const dispatch = (0, _store.useUIKitDispatch)();
  const styles = (0, _styles.useStyle)();
  const {
    isConnected
  } = (0, _useAuth.default)();
  const flatListRef = (0, _react.useRef)(null);
  const onNextPageRef = (0, _react.useRef)(null);
  const unsubscribeRef = (0, _react.useRef)(null);
  const getGlobalFeedList = (0, _react.useCallback)(() => {
    return (0, _feed.getGlobalFeed)({
      callback: ({
        data,
        loading,
        onNextPage
      }) => {
        if (!loading && data) {
          (0, _post.amityPostsFormatter)(data).then(posts => dispatch(setNewGlobalFeed(posts)));
        }
        onNextPageRef.current = onNextPage;
      }
    });
  }, [dispatch, setNewGlobalFeed]);
  const handleLoadMore = () => {
    var _onNextPageRef$curren;
    (_onNextPageRef$curren = onNextPageRef.current) === null || _onNextPageRef$curren === void 0 || _onNextPageRef$curren.call(onNextPageRef);
  };
  const onRefresh = (0, _react.useCallback)(async () => {
    var _unsubscribeRef$curre;
    if (!unsubscribeRef.current) return;
    (_unsubscribeRef$curre = unsubscribeRef.current) === null || _unsubscribeRef$curre === void 0 || _unsubscribeRef$curre.call(unsubscribeRef);
    unsubscribeRef.current = getGlobalFeedList();
    setRefreshing(false);
  }, [getGlobalFeedList]);
  (0, _native.useFocusEffect)((0, _react.useCallback)(() => {
    if (isConnected) {
      unsubscribeRef.current = getGlobalFeedList();
    }
  }, [isConnected, getGlobalFeedList]));
  const onDeletePost = async postId => {
    const isDeleted = await (0, _feed.deletePostById)(postId);
    if (isDeleted) {
      dispatch(deleteByPostId({
        postId
      }));
    }
  };
  return /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.feedWrap
  }, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.feedWrap
  }, /*#__PURE__*/React.createElement(_reactNative.FlatList, {
    data: postList,
    renderItem: ({
      item,
      index
    }) => /*#__PURE__*/React.createElement(_PostList.default, {
      onDelete: onDeletePost,
      postDetail: item,
      postIndex: index,
      isGlobalfeed: true
    }),
    keyExtractor: item => item.postId.toString(),
    onEndReachedThreshold: 0.5,
    onEndReached: handleLoadMore,
    ref: flatListRef,
    refreshControl: /*#__PURE__*/React.createElement(_reactNative.RefreshControl, {
      refreshing: refreshing,
      onRefresh: onRefresh,
      colors: ['lightblue'],
      tintColor: "lightblue"
    }),
    extraData: postList,
    ListHeaderComponent: /*#__PURE__*/React.createElement(_MyStories.default, null)
  })));
}
//# sourceMappingURL=index.js.map