"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _react = require("react");
var _reactNativeSvg = require("react-native-svg");
var _xml = require("../../../core/assets/icons/xml");
var _enums = require("../../enums");
var _hooks = require("../../hooks");
var _useConfig = _interopRequireDefault(require("../../hooks/useConfig"));
var _styles = require("./styles");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _reactNativePaper = require("react-native-paper");
var _assets = require("../../../core/assets");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const StoryCircleItem = ({
  onPressStoryView,
  storyTarget
}) => {
  var _getUiKitConfig;
  const theme = (0, _reactNativePaper.useTheme)();
  const [avatarUrl, setAvatarUrl] = (0, _react.useState)(null);
  const [communityData, setCommunityData] = (0, _react.useState)(null);
  const [communityLoading, setCommunityLoading] = (0, _react.useState)(true);
  const {
    hasStoryPermission
  } = (0, _hooks.useStoryPermission)(storyTarget.targetId);
  const {
    getImage
  } = (0, _hooks.useFile)();
  const {
    getUiKitConfig
  } = (0, _useConfig.default)();
  const styles = (0, _styles.useStyles)();
  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'] : ['#e2e2e2', '#e2e2e2'];
  (0, _react.useEffect)(() => {
    if (storyTarget.targetType !== 'community') return null;
    const unsubscribe = _tsSdkReactNative.CommunityRepository.getCommunity(storyTarget.targetId, async ({
      error,
      loading,
      data
    }) => {
      if (error) {
        setCommunityLoading(false);
        return;
      }
      if (!loading) {
        setCommunityData(data);
        setCommunityLoading(false);
        const avatarImage = await getImage({
          fileId: data.avatarFileId,
          imageSize: _enums.ImageSizeState.small,
          type: 'community'
        });
        setAvatarUrl(avatarImage);
      }
    });
    return () => unsubscribe();
  }, [getImage, storyTarget.targetId, storyTarget.targetType]);
  if (storyTarget.targetType !== 'community') return null;

  // Global story feed: only show stories from communities the user has joined.
  // Public community stories are visible on the community profile page,
  // but must NOT appear in the global story feed for non-members.
  // Private community stories are never returned by the SDK for non-members,
  // but we guard here as an extra safety net.
  if (!communityLoading && !(communityData !== null && communityData !== void 0 && communityData.isJoined)) return null;
  return /*#__PURE__*/React.createElement(_reactNative.TouchableOpacity, {
    key: storyTarget.targetId,
    style: styles.avatarContainer,
    onPress: () => onPressStoryView(storyTarget)
  }, /*#__PURE__*/React.createElement(_reactNative.Image, {
    source: avatarUrl ? {
      uri: avatarUrl
    } : {
      uri: _assets.defaultCommunityAvatarUri
    },
    style: styles.communityAvatar
  }), /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    style: styles.storyRing,
    width: 64,
    height: 64,
    xml: (0, _xml.storyRing)(storyRingColor[0], storyRingColor[1])
  }), hasStoryPermission && (storyTarget === null || storyTarget === void 0 ? void 0 : storyTarget.failedStoriesCount) > 0 ? /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.errorIcon
  }, /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    width: 16,
    height: 16,
    xml: (0, _xml.errorIcon)()
  })) : communityData !== null && communityData !== void 0 && communityData.isOfficial ? /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    style: styles.officialIcon,
    xml: (0, _xml.officialIcon)(theme.colors.primary)
  }) : null, /*#__PURE__*/React.createElement(_reactNative.View, {
    style: styles.textRow
  }, !(communityData !== null && communityData !== void 0 && communityData.isPublic) && /*#__PURE__*/React.createElement(_reactNativeSvg.SvgXml, {
    width: 17,
    height: 17,
    xml: (0, _xml.privateIcon)(theme.colors.base)
  }), /*#__PURE__*/React.createElement(_reactNative.Text, {
    numberOfLines: 1,
    ellipsizeMode: "tail",
    style: styles.itemText
  }, communityData === null || communityData === void 0 ? void 0 : communityData.displayName)));
};
var _default = exports.default = StoryCircleItem;
//# sourceMappingURL=StoryCircleItem.js.map