"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _styles = require("./styles");
var _useStory = require("../../hooks/useStory");
var _native = require("@react-navigation/native");
var _reactNativeSvg = require("react-native-svg");
var _xml = require("../../../core/assets/icons/xml");
var _useFile = require("../../hooks/useFile");
var _imageSizeState = require("../../enums/imageSizeState");
var _useStoryPermission = require("../../hooks/useStoryPermission");
var _useConfig = _interopRequireDefault(require("../../hooks/useConfig"));
var _enums = require("../../enums");
var _reactNativeModalbox = _interopRequireDefault(require("react-native-modalbox"));
var _View = _interopRequireDefault(require("../../features/story/View/View"));
var _Typography = require("../../../core/components/Typography/Typography");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const CommunityStories = ({
  communityId,
  community
}) => {
  var _getUiKitConfig;
  const navigation = (0, _native.useNavigation)();
  const styles = (0, _styles.useStyles)();
  const {
    getUiKitConfig
  } = (0, _useConfig.default)();
  const {
    hasStoryPermission
  } = (0, _useStoryPermission.useStoryPermission)(communityId);
  const {
    getStoryTarget,
    storyTarget,
    stories,
    getStories
  } = (0, _useStory.useStory)();
  const {
    getImage
  } = (0, _useFile.useFile)();
  const [avatarUrl, setAvatarUrl] = (0, _react.useState)(undefined);
  const [viewStory, setViewStory] = (0, _react.useState)(false);
  const shouldRestoreStory = (0, _react.useRef)(false);
  const storyRingColor = hasStoryPermission && (storyTarget === null || storyTarget === void 0 ? void 0 : storyTarget.failedStoriesCount) > 0 ? ['#DE1029', '#DE1029'] : storyTarget !== null && storyTarget !== void 0 && storyTarget.hasUnseen ? ((_getUiKitConfig = getUiKitConfig({
    page: _enums.PageID.StoryPage,
    component: _enums.ComponentID.StoryTab,
    element: _enums.ElementID.StoryRing
  })) === null || _getUiKitConfig === void 0 ? void 0 : _getUiKitConfig.progress_color) ?? ['#e2e2e2', '#e2e2e2'] : stories.length > 0 ? ['#e2e2e2', '#ffffff'] : ['#ffffff', '#ffffff'];
  (0, _react.useEffect)(() => {
    (async () => {
      const avatarImage = await getImage({
        fileId: community === null || community === void 0 ? void 0 : community.avatarFileId,
        imageSize: _imageSizeState.ImageSizeState.small,
        type: 'community'
      });
      setAvatarUrl(avatarImage);
    })();
  }, [community === null || community === void 0 ? void 0 : community.avatarFileId, getImage]);
  (0, _native.useFocusEffect)((0, _react.useCallback)(() => {
    if (shouldRestoreStory.current) {
      shouldRestoreStory.current = false;
      setViewStory(true);
      return;
    }
    getStoryTarget({
      targetId: communityId,
      targetType: 'community'
    });
    getStories({
      targetId: communityId,
      targetType: 'community'
    });
  }, [communityId, getStoryTarget, getStories]));
  const onPressCreateStory = (0, _react.useCallback)(() => {
    hasStoryPermission && navigation.navigate('CreateStory', {
      targetId: communityId,
      targetType: 'community'
    });
  }, [communityId, hasStoryPermission, navigation]);
  const onPressStoryView = (0, _react.useCallback)(() => {
    setViewStory(true);
  }, []);
  const onPressAvatar = (0, _react.useCallback)(() => {
    setViewStory(false);
    onPressCreateStory();
  }, [onPressCreateStory]);
  const onPressCommunityName = (0, _react.useCallback)(() => {
    setViewStory(false);
    navigation.navigate('CommunityProfilePage', {
      communityId: communityId
    });
  }, [communityId, navigation]);
  const renderCommunityStory = () => {
    if (community !== null && community !== void 0 && community.isJoined) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
        style: styles.avatarContainer,
        onPress: storyTarget !== null && storyTarget !== void 0 && storyTarget.hasUnseen || stories.length > 0 ? onPressStoryView : onPressCreateStory
      }, /*#__PURE__*/React.createElement(_reactNative.Image, {
        source: avatarUrl ? {
          uri: avatarUrl
        } : require('../../../core/assets/images/communityAvatar.png'),
        style: styles.communityAvatar
      }), /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
        style: styles.storyRing,
        width: 48,
        height: 48,
        xml: (0, _xml.storyRing)(storyRingColor[0], storyRingColor[1])
      }), hasStoryPermission ? (storyTarget === null || storyTarget === void 0 ? void 0 : storyTarget.failedStoriesCount) > 0 ? /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
        style: styles.errorIcon,
        xml: (0, _xml.errorIcon)()
      }) : /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
        onPress: () => onPressCreateStory()
      }, /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
        style: styles.storyCreateIcon,
        xml: (0, _xml.storyCircleCreatePlusIcon)()
      })) : null), /*#__PURE__*/React.createElement(_Typography.Typography.Caption, {
        style: styles.base
      }, "Story"));
    }
    if (community !== null && community !== void 0 && community.isPublic && !(community !== null && community !== void 0 && community.isJoined) && stories.length > 0) {
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
        style: styles.avatarContainer,
        onPress: onPressStoryView
      }, /*#__PURE__*/React.createElement(_reactNative.Image, {
        source: avatarUrl ? {
          uri: avatarUrl
        } : require('../../../core/assets/images/communityAvatar.png'),
        style: styles.communityAvatar
      }), /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
        style: styles.storyRing,
        width: 48,
        height: 48,
        xml: (0, _xml.storyRing)(storyRingColor[0], storyRingColor[1])
      })), /*#__PURE__*/React.createElement(_Typography.Typography.Caption, {
        style: styles.base
      }, "Story"));
    }
    return null;
  };
  const handleNavigateToUser = (0, _react.useCallback)(userId => {
    shouldRestoreStory.current = true;
    setViewStory(false);
    navigation.navigate('UserProfile', {
      userId
    });
  }, [navigation]);
  const onFinish = (0, _react.useCallback)(() => {
    setViewStory(false);
    getStoryTarget({
      targetId: communityId,
      targetType: 'community'
    });
  }, [communityId, getStoryTarget]);
  if (community !== null && community !== void 0 && community.isPublic && !(community !== null && community !== void 0 && community.isJoined) && stories.length <= 0) {
    return null;
  }
  if (!hasStoryPermission && stories.length === 0) return null;
  return /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.container
  }, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.storyItemWrap
  }, renderCommunityStory()), /*#__PURE__*/React.createElement(_reactNativeModalbox.default, {
    style: styles.modal,
    isOpen: viewStory,
    onClosed: () => setViewStory(false),
    position: "center",
    swipeToClose: true,
    swipeArea: 250,
    backButtonClose: true,
    coverScreen: true
  }, /*#__PURE__*/React.createElement(_View.default, {
    targetId: communityId,
    targetType: "community",
    onFinish: onFinish,
    onPressAvatar: onPressAvatar,
    onPressCommunityName: onPressCommunityName,
    onNavigateToUser: handleNavigateToUser
  })));
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(CommunityStories);
//# sourceMappingURL=index.js.map