import { Image, TouchableOpacity, View, Pressable } from 'react-native';
import { useEffect, useState, useCallback, Fragment, memo } from 'react';
import { FileRepository, RoomRepository } from '@amityco/ts-sdk-react-native';
import { useStyles } from './styles';
import { useNavigation } from '@react-navigation/native';
import { SvgXml } from 'react-native-svg';
import { play } from '../../../core/assets/icons';
import { Typography } from '../../../core/components/Typography/Typography';
import { RoomStatus } from '../../enums/roomStatus';
import LiveStreamEndThumbnail from './LivestreamEndedThumbnail';
import LiveStreamIdleThumbnail from './LivestreamIdleThumbnail';
import RenderTextWithMention from '../RenderTextWithMention/RenderTextWithMention';
import LiveStreamTerminatedThumbnail from './LivestreamTerminatedThumbnail';
const LivestreamContent = ({
  post,
  roomId,
  onPressPost
}) => {
  var _livestream$moderatio, _livestream$moderatio2;
  const {
    styles,
    theme
  } = useStyles();
  const navigation = useNavigation();
  const [error, setError] = useState(false);
  const [livestream, setLivestream] = useState();
  const [thumbnailUrl, setThumbnailUrl] = useState();
  const [isUpcoming, setIsUpcoming] = useState(false);
  const onPlayLivestream = useCallback(() => {
    navigation.navigate('LivestreamPlayer', {
      post,
      roomId: livestream.roomId
    });
  }, [livestream, navigation, post]);
  const getLivestreamThumbnail = async currentStream => {
    const defaultThumbnail = require('../../../core/assets/images/livestream.png');
    if (currentStream.thumbnailFileId) {
      const file = await FileRepository.getFile(currentStream.thumbnailFileId);
      if (file) {
        const fileUrl = FileRepository.fileUrlWithSize(file.data.fileUrl, 'full');
        setThumbnailUrl({
          uri: fileUrl
        });
        return;
      }
    }
    setThumbnailUrl(defaultThumbnail);
  };
  useEffect(() => {
    setIsUpcoming(true);
    const unsubscribe = RoomRepository.getRoom(roomId, ({
      data,
      loading,
      error: streamError
    }) => {
      if (streamError) setError(!!streamError);
      if (!loading && data) {
        setLivestream(data);
        getLivestreamThumbnail(data);
      }
      setIsUpcoming(loading);
    });
    return () => {
      unsubscribe();
    };
  }, [roomId]);
  if (!livestream) return null;
  if (error || livestream !== null && livestream !== void 0 && livestream.isDeleted) {
    return /*#__PURE__*/React.createElement(View, {
      key: livestream.roomId,
      style: styles.container
    }, /*#__PURE__*/React.createElement(LiveStreamIdleThumbnail, null));
  }
  const isTerminated = (livestream === null || livestream === void 0 || (_livestream$moderatio = livestream.moderation) === null || _livestream$moderatio === void 0 ? void 0 : _livestream$moderatio.terminateLabels) && (livestream === null || livestream === void 0 || (_livestream$moderatio2 = livestream.moderation) === null || _livestream$moderatio2 === void 0 || (_livestream$moderatio2 = _livestream$moderatio2.terminateLabels) === null || _livestream$moderatio2 === void 0 ? void 0 : _livestream$moderatio2.length) > 0;
  const isLiveOrEnded = (livestream === null || livestream === void 0 ? void 0 : livestream.status) === RoomStatus.live || (livestream === null || livestream === void 0 ? void 0 : livestream.status) === RoomStatus.ended;
  if (isTerminated && isLiveOrEnded) {
    return /*#__PURE__*/React.createElement(View, {
      key: livestream.roomId,
      style: styles.container
    }, /*#__PURE__*/React.createElement(LiveStreamTerminatedThumbnail, null));
  }
  return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(Pressable, {
    onPress: onPressPost,
    style: styles.info
  }, /*#__PURE__*/React.createElement(RenderTextWithMention, {
    isTitle: true,
    mentionPositionArr: [],
    textPost: livestream.title
  }), livestream.description && /*#__PURE__*/React.createElement(RenderTextWithMention, {
    mentionPositionArr: [],
    textPost: livestream.description
  })), /*#__PURE__*/React.createElement(View, {
    style: styles.container
  }, (livestream.status === RoomStatus.idle || isUpcoming) && thumbnailUrl && /*#__PURE__*/React.createElement(View, {
    style: styles.content
  }, /*#__PURE__*/React.createElement(Image, {
    source: thumbnailUrl,
    style: styles.streamImageCover
  }), /*#__PURE__*/React.createElement(View, {
    style: styles.streamStatusRecorded
  }, /*#__PURE__*/React.createElement(Typography.CaptionBold, {
    style: styles.streamStatusText
  }, "UPCOMING LIVE"))), livestream.status === RoomStatus.ended && /*#__PURE__*/React.createElement(LiveStreamEndThumbnail, null), (livestream.status === RoomStatus.live || livestream.status === RoomStatus.recorded || livestream.status === RoomStatus.waitingReconnect) && thumbnailUrl && !isUpcoming && /*#__PURE__*/React.createElement(View, {
    style: styles.content
  }, /*#__PURE__*/React.createElement(Image, {
    source: thumbnailUrl,
    style: styles.streamImageCover
  }), (livestream.status === RoomStatus.live || livestream.status === RoomStatus.waitingReconnect) && /*#__PURE__*/React.createElement(View, {
    style: styles.streamStatusLive
  }, /*#__PURE__*/React.createElement(Typography.CaptionBold, {
    style: styles.streamStatusText
  }, "LIVE")), livestream.status === RoomStatus.recorded && /*#__PURE__*/React.createElement(View, {
    style: styles.streamStatusRecorded
  }, /*#__PURE__*/React.createElement(Typography.CaptionBold, {
    style: styles.streamStatusText
  }, "RECORDED")), /*#__PURE__*/React.createElement(TouchableOpacity, {
    onPress: onPlayLivestream,
    style: styles.streamPlayButton
  }, /*#__PURE__*/React.createElement(SvgXml, {
    width: "40",
    height: "40",
    xml: play(),
    color: theme.colors.background
  })))));
};
export default /*#__PURE__*/memo(LivestreamContent);
//# sourceMappingURL=index.js.map