"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _react = require("react");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _styles = require("./styles");
var _native = require("@react-navigation/native");
var _reactNativeSvg = require("react-native-svg");
var _icons = require("../../../core/assets/icons");
var _Typography = require("../../../core/components/Typography/Typography");
var _roomStatus = require("../../enums/roomStatus");
var _LivestreamEndedThumbnail = _interopRequireDefault(require("./LivestreamEndedThumbnail"));
var _LivestreamIdleThumbnail = _interopRequireDefault(require("./LivestreamIdleThumbnail"));
var _RenderTextWithMention = _interopRequireDefault(require("../RenderTextWithMention/RenderTextWithMention"));
var _LivestreamTerminatedThumbnail = _interopRequireDefault(require("./LivestreamTerminatedThumbnail"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const LivestreamContent = ({
  post,
  roomId,
  onPressPost
}) => {
  var _livestream$moderatio, _livestream$moderatio2;
  const {
    styles,
    theme
  } = (0, _styles.useStyles)();
  const navigation = (0, _native.useNavigation)();
  const [error, setError] = (0, _react.useState)(false);
  const [livestream, setLivestream] = (0, _react.useState)();
  const [thumbnailUrl, setThumbnailUrl] = (0, _react.useState)();
  const [isUpcoming, setIsUpcoming] = (0, _react.useState)(false);
  const onPlayLivestream = (0, _react.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 _tsSdkReactNative.FileRepository.getFile(currentStream.thumbnailFileId);
      if (file) {
        const fileUrl = _tsSdkReactNative.FileRepository.fileUrlWithSize(file.data.fileUrl, 'full');
        setThumbnailUrl({
          uri: fileUrl
        });
        return;
      }
    }
    setThumbnailUrl(defaultThumbnail);
  };
  (0, _react.useEffect)(() => {
    setIsUpcoming(true);
    const unsubscribe = _tsSdkReactNative.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(_reactNative.View, {
      key: livestream.roomId,
      style: styles.container
    }, /*#__PURE__*/React.createElement(_LivestreamIdleThumbnail.default, 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.RoomStatus.live || (livestream === null || livestream === void 0 ? void 0 : livestream.status) === _roomStatus.RoomStatus.ended;
  if (isTerminated && isLiveOrEnded) {
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      key: livestream.roomId,
      style: styles.container
    }, /*#__PURE__*/React.createElement(_LivestreamTerminatedThumbnail.default, null));
  }
  return /*#__PURE__*/React.createElement(_react.Fragment, null, /*#__PURE__*/React.createElement(_reactNative.Pressable, {
    onPress: onPressPost,
    style: styles.info
  }, /*#__PURE__*/React.createElement(_RenderTextWithMention.default, {
    isTitle: true,
    mentionPositionArr: [],
    textPost: livestream.title
  }), livestream.description && /*#__PURE__*/React.createElement(_RenderTextWithMention.default, {
    mentionPositionArr: [],
    textPost: livestream.description
  })), /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.container
  }, (livestream.status === _roomStatus.RoomStatus.idle || isUpcoming) && thumbnailUrl && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.content
  }, /*#__PURE__*/React.createElement(_reactNative.Image, {
    source: thumbnailUrl,
    style: styles.streamImageCover
  }), /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.streamStatusRecorded
  }, /*#__PURE__*/React.createElement(_Typography.Typography.CaptionBold, {
    style: styles.streamStatusText
  }, "UPCOMING LIVE"))), livestream.status === _roomStatus.RoomStatus.ended && /*#__PURE__*/React.createElement(_LivestreamEndedThumbnail.default, null), (livestream.status === _roomStatus.RoomStatus.live || livestream.status === _roomStatus.RoomStatus.recorded || livestream.status === _roomStatus.RoomStatus.waitingReconnect) && thumbnailUrl && !isUpcoming && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.content
  }, /*#__PURE__*/React.createElement(_reactNative.Image, {
    source: thumbnailUrl,
    style: styles.streamImageCover
  }), (livestream.status === _roomStatus.RoomStatus.live || livestream.status === _roomStatus.RoomStatus.waitingReconnect) && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.streamStatusLive
  }, /*#__PURE__*/React.createElement(_Typography.Typography.CaptionBold, {
    style: styles.streamStatusText
  }, "LIVE")), livestream.status === _roomStatus.RoomStatus.recorded && /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.streamStatusRecorded
  }, /*#__PURE__*/React.createElement(_Typography.Typography.CaptionBold, {
    style: styles.streamStatusText
  }, "RECORDED")), /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    onPress: onPlayLivestream,
    style: styles.streamPlayButton
  }, /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    width: "40",
    height: "40",
    xml: (0, _icons.play)(),
    color: theme.colors.background
  })))));
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(LivestreamContent);
//# sourceMappingURL=index.js.map