import { ActivityIndicator, View } from 'react-native';
import { memo, useCallback, useLayoutEffect, useState } from 'react';
import { useFile, useStory, useStoryPermission } from '../../../hooks';
import { useStyles } from './styles';
import { isCommunityModerator } from '../../../../core/utils/permission';
import useAuth from '../../../../core/hooks/useAuth';
import { ImageSizeState } from '../../../enums';
import { CommunityRepository } from '@amityco/ts-sdk-react-native';
import AmityViewStoryItem from './components/AmityViewStoryItem';
import { defaultCommunityAvatarUri } from '../../../../core/assets';
const AmityViewStoryPage = ({
  targetId,
  targetType,
  onFinish,
  onPressCommunityName,
  onPressAvatar,
  currentPage,
  index,
  onNavigateToUser
}) => {
  var _currentStory$creator;
  const {
    client
  } = useAuth();
  const userId = client.userId;
  const {
    hasStoryPermission
  } = useStoryPermission(targetId);
  const styles = useStyles();
  const {
    getStories,
    stories,
    loading
  } = useStory();
  const {
    getImage
  } = useFile();
  const [storyData, setStoryData] = useState([]);
  const [current, setCurrent] = useState(0);
  const currentStory = storyData[current];
  const isOwner = (currentStory === null || currentStory === void 0 || (_currentStory$creator = currentStory.creator) === null || _currentStory$creator === void 0 ? void 0 : _currentStory$creator.userId) === userId;
  const [communityData, setCommunityData] = useState(null);
  const [communityAvatar, setCommunityAvatar] = useState(null);
  const [isModerator, setIsModerator] = useState(false);
  const hasStoryImpressionPermission = isModerator || isOwner;
  useLayoutEffect(() => {
    if (targetId && index === currentPage) {
      getStories({
        targetId: targetId,
        targetType: targetType,
        options: {
          orderBy: 'asc',
          sortBy: 'createdAt'
        }
      });
    }
  }, [currentPage, getStories, index, targetId, targetType]);
  useLayoutEffect(() => {
    if (stories) {
      const unSeenStory = stories === null || stories === void 0 ? void 0 : stories.findIndex(story => !story.isSeen);
      if (unSeenStory > 0) {
        setCurrent(unSeenStory);
      }
      const mappedStory = stories.map((story, i) => ({
        ...story,
        finish: i < unSeenStory ? 1 : 0
      }));
      setStoryData(mappedStory);
    }
  }, [stories]);
  useLayoutEffect(() => {
    if (!targetId || !userId) return;
    (async () => {
      const isMod = await isCommunityModerator({
        userId,
        communityId: targetId
      });
      setIsModerator(isMod);
    })();
  }, [targetId, userId]);
  useLayoutEffect(() => {
    if (!targetId) return;
    CommunityRepository.getCommunity(targetId, async ({
      error: err,
      loading: fetching,
      data
    }) => {
      if (err) return;
      if (!fetching) {
        setCommunityData(data);
        const avatarUrl = await getImage({
          fileId: data.avatarFileId,
          imageSize: ImageSizeState.small,
          type: 'community'
        });
        setCommunityAvatar(avatarUrl ?? defaultCommunityAvatarUri);
      }
    });
  }, [getImage, targetId]);
  useLayoutEffect(() => {
    var _storyData$current;
    if (!(storyData !== null && storyData !== void 0 && storyData.length)) return;
    (_storyData$current = storyData[current]) === null || _storyData$current === void 0 || _storyData$current.analytics.markAsSeen();
  }, [current, storyData]);
  const close = useCallback(state => {
    onFinish && onFinish(state, targetId);
  }, [onFinish, targetId]);
  const onClose = useCallback(() => {
    onFinish && onFinish();
  }, [onFinish]);
  if (!targetId) return null;
  if (loading || !currentStory || !communityData || !storyData) {
    return /*#__PURE__*/React.createElement(View, {
      style: styles.spinnerContainer
    }, /*#__PURE__*/React.createElement(ActivityIndicator, {
      animating: loading,
      size: "large",
      color: 'white'
    }));
  }
  if (currentPage === index) {
    return /*#__PURE__*/React.createElement(AmityViewStoryItem, {
      key: storyData[current].storyId,
      communityData: communityData,
      communityAvatar: communityAvatar,
      storyData: storyData,
      hasStoryImpressionPermission: hasStoryImpressionPermission,
      close: close,
      onClose: onClose,
      setCurrent: setCurrent,
      current: current,
      hasStoryPermission: hasStoryPermission,
      onPressAvatar: onPressAvatar,
      onPressCommunityName: onPressCommunityName,
      onNavigateToUser: onNavigateToUser
    });
  }
  return null;
};
export default /*#__PURE__*/memo(AmityViewStoryPage);
//# sourceMappingURL=View.js.map