"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _reactNativeSvg = require("react-native-svg");
var _styles = require("./styles");
var _useAuth = _interopRequireDefault(require("../../../core/hooks/useAuth"));
var _feed = require("../../../core/legacy/feed");
var _dist = _interopRequireDefault(require("../legacy/react-native-image-viewing/dist"));
var _store = require("../../../core/stores/store");
var _xml = require("../../../core/assets/icons/xml");
var _LivestreamContent = _interopRequireDefault(require("../LivestreamContent"));
var _PreviewLink = require("../PreviewLink");
var _RenderTextWithMention = _interopRequireDefault(require("../RenderTextWithMention/RenderTextWithMention"));
var _PollContent = _interopRequireDefault(require("../PollContent"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const PostContent = ({
  post,
  textPost,
  onPressPost,
  disabledPoll,
  childrenPosts,
  showedAllOptions,
  mentionPositionArr
}) => {
  const {
    apiRegion
  } = (0, _useAuth.default)();
  const [imagePosts, setImagePosts] = (0, _react.useState)([]);
  const [videoPosts, setVideoPosts] = (0, _react.useState)([]);
  const [pollIds, setPollIds] = (0, _react.useState)([]);
  const [livestreamId, setLivestreamId] = (0, _react.useState)([]);
  const [imagePostsFullSize, setImagePostsFullSize] = (0, _react.useState)([]);
  const [videoPostsFullSize, setVideoPostsFullSize] = (0, _react.useState)([]);
  const [visibleFullImage, setIsVisibleFullImage] = (0, _react.useState)(false);
  const [imageIndex, setImageIndex] = (0, _react.useState)(0);
  const styles = (0, _styles.useStyles)();
  let imageStyle = styles.imageLargePost;
  let colStyle = styles.col2;
  const {
    currentPostdetail
  } = (0, _store.useUIKitSelector)(state => state.postDetail);
  const {
    postList: postListGlobal
  } = (0, _store.useUIKitSelector)(state => state.globalFeed);
  const {
    postList
  } = (0, _store.useUIKitSelector)(state => state.feed);
  (0, _react.useEffect)(() => {
    setImagePostsFullSize([]);
    setVideoPostsFullSize([]);
    if (imagePosts.length > 0) {
      const updatedUrls = imagePosts.map(url => {
        return {
          uri: url.replace('size=medium', 'size=large')
        };
      });
      setImagePostsFullSize(updatedUrls);
    }
    if (videoPosts.length > 0) {
      const updatedUrls = videoPosts.map(item => {
        return {
          uri: `https://api.${apiRegion}.amity.co/api/v3/files/${item === null || item === void 0 ? void 0 : item.thumbnailFileId}/download?size=large`
        };
      });
      setVideoPostsFullSize(updatedUrls);
    }
  }, [imagePosts, videoPosts, apiRegion]);
  const getPostInfo = (0, _react.useCallback)(async () => {
    try {
      const response = await Promise.all(childrenPosts.map(async id => {
        const {
          data: childrenPost
        } = await (0, _feed.getPostById)(id);
        return {
          dataType: childrenPost === null || childrenPost === void 0 ? void 0 : childrenPost.dataType,
          data: childrenPost === null || childrenPost === void 0 ? void 0 : childrenPost.data
        };
      }));
      const images = [];
      const videos = [];
      const polls = [];
      const livestreams = [];
      response.forEach(item => {
        var _item$data, _item$data2;
        if ((item === null || item === void 0 ? void 0 : item.dataType) === 'image' && item !== null && item !== void 0 && (_item$data = item.data) !== null && _item$data !== void 0 && _item$data.fileId) {
          const url = `https://api.${apiRegion}.amity.co/api/v3/files/${item === null || item === void 0 ? void 0 : item.data.fileId}/download?size=medium`;
          if (!images.includes(url)) {
            images.push(url);
          }
        } else if ((item === null || item === void 0 ? void 0 : item.dataType) === 'video' && item !== null && item !== void 0 && (_item$data2 = item.data) !== null && _item$data2 !== void 0 && _item$data2.videoFileId.original) {
          const isExisted = videos.some(video => video.videoFileId.original === item.data.videoFileId.original);
          if (!isExisted) {
            videos.push(item.data);
          }
        } else if ((item === null || item === void 0 ? void 0 : item.dataType) === 'poll') {
          if (!polls.some(poll => poll.pollId === item.data.pollId)) {
            polls.push(item.data);
          }
        } else if ((item === null || item === void 0 ? void 0 : item.dataType) === 'room') {
          if (!livestreams.includes(item.data.roomId)) {
            livestreams.push(item.data.roomId);
          }
        }
      });
      if (images.length > 0) {
        setImagePosts(images);
      }
      if (videos.length > 0) {
        setVideoPosts(videos);
      }
      if (polls.length > 0) {
        setPollIds(polls);
      }
      if (livestreams.length > 0) {
        setLivestreamId(livestreams);
      }
    } catch (error) {
      console.log('error: ', error);
    }
  }, [apiRegion, childrenPosts]);
  (0, _react.useEffect)(() => {
    setVideoPosts([]);
    setImagePosts([]);
    getPostInfo();
  }, [childrenPosts, currentPostdetail, postList, postListGlobal, getPostInfo]);
  function onClickImage(index) {
    setIsVisibleFullImage(true);
    setImageIndex(index);
  }
  function renderMediaPosts() {
    const thumbnailFileIds = videoPosts.length > 0 ? videoPosts.map(item => {
      return `https://api.${apiRegion}.amity.co/api/v3/files/${item === null || item === void 0 ? void 0 : item.thumbnailFileId}/download?size=medium`;
    }) : [];
    const mediaPosts = [...imagePosts].length > 0 ? [...imagePosts] : [...thumbnailFileIds];
    const imageElement = mediaPosts.map((item, index) => {
      if (mediaPosts.length === 1) {
        imageStyle = styles.imageLargePost;
        colStyle = styles.col6;
      } else if (mediaPosts.length === 2) {
        colStyle = styles.col3;
        if (index === 0) {
          imageStyle = [styles.imageLargePost, styles.imageMarginRight];
        } else {
          imageStyle = [styles.imageLargePost, styles.imageMarginLeft];
        }
      } else if (mediaPosts.length === 3) {
        switch (index) {
          case 0:
            colStyle = styles.col6;
            imageStyle = [styles.imageMediumPost, styles.imageMarginBottom];
            break;
          case 1:
            colStyle = styles.col3;
            imageStyle = [styles.imageMediumPost, styles.imageMarginTop, styles.imageMarginRight];
            break;
          case 2:
            colStyle = styles.col3;
            imageStyle = [styles.imageMediumPost, styles.imageMarginTop, styles.imageMarginLeft];
            break;
          default:
            break;
        }
      } else {
        switch (index) {
          case 0:
            colStyle = styles.col6;
            imageStyle = [styles.imageMediumLargePost, styles.imageMarginBottom];
            break;
          case 1:
            colStyle = styles.col2;
            imageStyle = [styles.imageSmallPost, styles.imageMarginTop, styles.imageMarginRight];
            break;
          case 2:
            colStyle = styles.col2;
            imageStyle = [styles.imageSmallPost, styles.imageMarginTop, styles.imageMarginLeft, styles.imageMarginRight];
            break;
          case 3:
            colStyle = styles.col2;
            imageStyle = [styles.imageSmallPost, styles.imageMarginTop, styles.imageMarginLeft];
            break;
          default:
            break;
        }
      }
      return /*#__PURE__*/React.createElement(_reactNative.View, {
        style: colStyle,
        key: item
      }, /*#__PURE__*/React.createElement(_reactNative.TouchableWithoutFeedback, {
        onPress: () => onClickImage(index)
      }, /*#__PURE__*/React.createElement(_reactNative.View, null, videoPosts.length > 0 && renderPlayButton(), /*#__PURE__*/React.createElement(_reactNative.Image, {
        style: imageStyle,
        source: {
          uri: item
        }
      }), index === 3 && imagePosts.length > 4 && /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.overlay
      }, /*#__PURE__*/React.createElement(_reactNative.Text, {
        style: styles.overlayText
      }, `+ ${imagePosts.length - 3}`)))));
    });
    if (imageElement.length < 3) {
      return /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.imagesWrap
      }, /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.row
      }, imageElement));
    } else if (imageElement.length === 3) {
      return /*#__PURE__*/React.createElement(_reactNative.View, {
        style: [styles.imagesWrap]
      }, /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.row
      }, imageElement.slice(0, 1)), /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.row
      }, imageElement.slice(1, 3)));
    } else {
      return /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.imagesWrap
      }, /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.row
      }, imageElement.slice(0, 1)), /*#__PURE__*/React.createElement(_reactNative.View, {
        style: styles.row
      }, imageElement.slice(1, 4)));
    }
  }
  function renderPlayButton() {
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.playButton
    }, /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
      xml: _xml.playBtn,
      width: "50",
      height: "50"
    }));
  }
  function renderImageHeader({
    imageIndex: imgIndex
  }) {
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.headerContainer
    }, /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.flexWidth
    }, /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
      style: styles.closebtnIcon,
      onPress: () => setIsVisibleFullImage(false)
    }, /*#__PURE__*/React.createElement(_reactNative.Text, {
      style: styles.closeBtn
    }, "X"))), /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.flexWidth
    }, /*#__PURE__*/React.createElement(_reactNative.Text, {
      style: styles.header
    }, imgIndex + 1, "/", imagePostsFullSize.length || videoPostsFullSize.length)), /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.flexWidth
    }));
  }
  return /*#__PURE__*/React.createElement(_react.Fragment, null, livestreamId.length <= 0 && /*#__PURE__*/React.createElement(_reactNative.Pressable, {
    onPress: onPressPost
  }, textPost && (childrenPosts === null || childrenPosts === void 0 ? void 0 : childrenPosts.length) === 0 && /*#__PURE__*/React.createElement(_PreviewLink.LinkPreview, {
    text: textPost,
    mentionPositionArr: [...mentionPositionArr]
  }), textPost && (childrenPosts === null || childrenPosts === void 0 ? void 0 : childrenPosts.length) > 0 && /*#__PURE__*/React.createElement(_RenderTextWithMention.default, {
    textPost: textPost,
    mentionPositionArr: [...mentionPositionArr]
  })), pollIds.length > 0 ? /*#__PURE__*/React.createElement(_PollContent.default, {
    post: post,
    pollId: pollIds[0].pollId,
    disabledPoll: disabledPoll,
    showedAllOptions: showedAllOptions
  }) : livestreamId.length > 0 ? /*#__PURE__*/React.createElement(_LivestreamContent.default, {
    post: post,
    onPressPost: onPressPost,
    roomId: livestreamId[0]
  }) : renderMediaPosts(), /*#__PURE__*/React.createElement(_dist.default, {
    images: imagePostsFullSize.length > 0 ? imagePostsFullSize : videoPostsFullSize,
    imageIndex: imageIndex,
    visible: visibleFullImage,
    onRequestClose: () => setIsVisibleFullImage(false),
    isVideoButton: videoPosts.length > 0 ? true : false,
    videoPosts: videoPosts,
    HeaderComponent: renderImageHeader
  }));
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(PostContent);
//# sourceMappingURL=index.js.map