import { Platform } from 'react-native';
import { useCallback, useLayoutEffect, useRef } from 'react';
import AndroidCubeEffect from '../StoryKit/src/components/AndroidCubeEffect';
import CubeNavigationHorizontal from '../StoryKit/src/components/CubeNavigationHorizontal';
import AmityViewStoryPage from '../../features/story/View/View';
import { useNavigation } from '@react-navigation/native';
import { useStoryPermission } from '../../hooks';
const StoryTargetView = ({
  currentCommunityIndex,
  setCurrentCommunityIndex,
  globalStoryTargets,
  setViewStory,
  onClose
}) => {
  var _cube$current;
  const cube = useRef(null);
  const communityId = globalStoryTargets[currentCommunityIndex].targetId;
  const {
    hasStoryPermission
  } = useStoryPermission(communityId);
  const navigation = useNavigation();
  useLayoutEffect(() => {
    cube && cube.current.scrollTo(currentCommunityIndex);
  }, [currentCommunityIndex]);
  cube && (cube === null || cube === void 0 || (_cube$current = cube.current) === null || _cube$current === void 0 ? void 0 : _cube$current.scrollTo(currentCommunityIndex));
  const onPressCreateStory = useCallback(() => {
    hasStoryPermission && navigation.navigate('CreateStory', {
      targetId: communityId,
      targetType: 'community'
    });
  }, [communityId, hasStoryPermission, navigation]);
  const onPressAvatar = useCallback(() => {
    setViewStory(false);
    onPressCreateStory();
  }, [onPressCreateStory, setViewStory]);
  const onPressCommunityName = useCallback(() => {
    setViewStory(false);
    navigation.navigate('CommunityProfilePage', {
      communityId: communityId
    });
  }, [communityId, navigation, setViewStory]);
  const onFinish = useCallback(state => {
    if (state === 'next') {
      if (currentCommunityIndex < globalStoryTargets.length - 1) {
        return setCurrentCommunityIndex(currentCommunityIndex + 1);
      }
      setViewStory(false);
      onClose && onClose();
      return;
    }
    if (state === 'previous') {
      if (currentCommunityIndex > 0) return setCurrentCommunityIndex(currentCommunityIndex - 1);
    }
    setViewStory(false);
    onClose && onClose();
  }, [currentCommunityIndex, globalStoryTargets.length, onClose, setCurrentCommunityIndex, setViewStory]);
  const handleNavigateToUser = useCallback(userId => {
    setViewStory(false);
    navigation.navigate('UserProfile', {
      userId
    });
    const unsubscribe = navigation.addListener('focus', () => {
      unsubscribe();
      setViewStory(true);
    });
  }, [navigation, setViewStory]);
  if (Platform.OS === 'ios') {
    return /*#__PURE__*/React.createElement(CubeNavigationHorizontal, {
      ref: cube,
      callBackAfterSwipe: x => {
        if (x !== currentCommunityIndex) {
          setCurrentCommunityIndex(parseInt(x, 10));
        }
      }
    }, globalStoryTargets.map((storyTarget, i) => {
      return /*#__PURE__*/React.createElement(AmityViewStoryPage, {
        targetType: 'community',
        targetId: storyTarget.targetId,
        key: storyTarget.targetId,
        onFinish: onFinish,
        onPressAvatar: onPressAvatar,
        onPressCommunityName: onPressCommunityName,
        onNavigateToUser: handleNavigateToUser,
        index: i,
        currentPage: currentCommunityIndex
      });
    }));
  } else {
    return /*#__PURE__*/React.createElement(AndroidCubeEffect, {
      ref: cube,
      callBackAfterSwipe: x => {
        if (x !== currentCommunityIndex) {
          setCurrentCommunityIndex(parseInt(x, 10));
        }
      }
    }, globalStoryTargets.map((storyTarget, i) => {
      return /*#__PURE__*/React.createElement(AmityViewStoryPage, {
        targetType: 'community',
        targetId: storyTarget.targetId,
        key: storyTarget.targetId,
        onFinish: onFinish,
        onPressAvatar: onPressAvatar,
        onPressCommunityName: onPressCommunityName,
        onNavigateToUser: handleNavigateToUser,
        index: i,
        currentPage: currentCommunityIndex
      });
    }));
  }
};
export default StoryTargetView;
//# sourceMappingURL=StoryTargetView.js.map