import { useCallback, useRef, useState } from 'react';

// import { useTranslation } from 'react-i18next';

import { FlatList, View } from 'react-native';
import { deletePostById, getGlobalFeed } from '../../../core/legacy/feed';
import useAuth from '../../../core/hooks/useAuth';
import PostList from '../../components/legacy/Social/PostList';
import { useStyle } from './styles';
import { amityPostsFormatter } from '../../../core/utils/post';
import globalFeedSlice from '../../../core/stores/slices/globalfeedSlice';
import { useUIKitDispatch, useUIKitSelector } from '../../../core/stores/store';
import { useFocusEffect } from '@react-navigation/native';
import { RefreshControl } from 'react-native';
import MyStories from '../../components/legacy/MyStories';
export default function GlobalFeed() {
  const {
    postList
  } = useUIKitSelector(state => state.globalFeed);
  const [refreshing, setRefreshing] = useState(false);
  const {
    deleteByPostId,
    setNewGlobalFeed
  } = globalFeedSlice.actions;
  const dispatch = useUIKitDispatch();
  const styles = useStyle();
  const {
    isConnected
  } = useAuth();
  const flatListRef = useRef(null);
  const onNextPageRef = useRef(null);
  const unsubscribeRef = useRef(null);
  const getGlobalFeedList = useCallback(() => {
    return getGlobalFeed({
      callback: ({
        data,
        loading,
        onNextPage
      }) => {
        if (!loading && data) {
          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 = 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]);
  useFocusEffect(useCallback(() => {
    if (isConnected) {
      unsubscribeRef.current = getGlobalFeedList();
    }
  }, [isConnected, getGlobalFeedList]));
  const onDeletePost = async postId => {
    const isDeleted = await deletePostById(postId);
    if (isDeleted) {
      dispatch(deleteByPostId({
        postId
      }));
    }
  };
  return /*#__PURE__*/React.createElement(View, {
    style: styles.feedWrap
  }, /*#__PURE__*/React.createElement(View, {
    style: styles.feedWrap
  }, /*#__PURE__*/React.createElement(FlatList, {
    data: postList,
    renderItem: ({
      item,
      index
    }) => /*#__PURE__*/React.createElement(PostList, {
      onDelete: onDeletePost,
      postDetail: item,
      postIndex: index,
      isGlobalfeed: true
    }),
    keyExtractor: item => item.postId.toString(),
    onEndReachedThreshold: 0.5,
    onEndReached: handleLoadMore,
    ref: flatListRef,
    refreshControl: /*#__PURE__*/React.createElement(RefreshControl, {
      refreshing: refreshing,
      onRefresh: onRefresh,
      colors: ['lightblue'],
      tintColor: "lightblue"
    }),
    extraData: postList,
    ListHeaderComponent: /*#__PURE__*/React.createElement(MyStories, null)
  })));
}
//# sourceMappingURL=index.js.map