"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _react = require("react");
var _hooks = require("../../../hooks");
var _styles = require("./styles");
var _permission = require("../../../../core/utils/permission");
var _useAuth = _interopRequireDefault(require("../../../../core/hooks/useAuth"));
var _enums = require("../../../enums");
var _tsSdkReactNative = require("@amityco/ts-sdk-react-native");
var _AmityViewStoryItem = _interopRequireDefault(require("./components/AmityViewStoryItem"));
var _assets = require("../../../../core/assets");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const AmityViewStoryPage = ({
  targetId,
  targetType,
  onFinish,
  onPressCommunityName,
  onPressAvatar,
  currentPage,
  index,
  onNavigateToUser
}) => {
  var _currentStory$creator;
  const {
    client
  } = (0, _useAuth.default)();
  const userId = client.userId;
  const {
    hasStoryPermission
  } = (0, _hooks.useStoryPermission)(targetId);
  const styles = (0, _styles.useStyles)();
  const {
    getStories,
    stories,
    loading
  } = (0, _hooks.useStory)();
  const {
    getImage
  } = (0, _hooks.useFile)();
  const [storyData, setStoryData] = (0, _react.useState)([]);
  const [current, setCurrent] = (0, _react.useState)(0);
  const currentStory = storyData[current];
  const isOwner = (currentStory === null || currentStory === void 0 || (_currentStory$creator = currentStory.creator) === null || _currentStory$creator === void 0 ? void 0 : _currentStory$creator.userId) === userId;
  const [communityData, setCommunityData] = (0, _react.useState)(null);
  const [communityAvatar, setCommunityAvatar] = (0, _react.useState)(null);
  const [isModerator, setIsModerator] = (0, _react.useState)(false);
  const hasStoryImpressionPermission = isModerator || isOwner;
  (0, _react.useLayoutEffect)(() => {
    if (targetId && index === currentPage) {
      getStories({
        targetId: targetId,
        targetType: targetType,
        options: {
          orderBy: 'asc',
          sortBy: 'createdAt'
        }
      });
    }
  }, [currentPage, getStories, index, targetId, targetType]);
  (0, _react.useLayoutEffect)(() => {
    if (stories) {
      const unSeenStory = stories === null || stories === void 0 ? void 0 : stories.findIndex(story => !story.isSeen);
      if (unSeenStory > 0) {
        setCurrent(unSeenStory);
      }
      const mappedStory = stories.map((story, i) => ({
        ...story,
        finish: i < unSeenStory ? 1 : 0
      }));
      setStoryData(mappedStory);
    }
  }, [stories]);
  (0, _react.useLayoutEffect)(() => {
    if (!targetId || !userId) return;
    (async () => {
      const isMod = await (0, _permission.isCommunityModerator)({
        userId,
        communityId: targetId
      });
      setIsModerator(isMod);
    })();
  }, [targetId, userId]);
  (0, _react.useLayoutEffect)(() => {
    if (!targetId) return;
    _tsSdkReactNative.CommunityRepository.getCommunity(targetId, async ({
      error: err,
      loading: fetching,
      data
    }) => {
      if (err) return;
      if (!fetching) {
        setCommunityData(data);
        const avatarUrl = await getImage({
          fileId: data.avatarFileId,
          imageSize: _enums.ImageSizeState.small,
          type: 'community'
        });
        setCommunityAvatar(avatarUrl ?? _assets.defaultCommunityAvatarUri);
      }
    });
  }, [getImage, targetId]);
  (0, _react.useLayoutEffect)(() => {
    var _storyData$current;
    if (!(storyData !== null && storyData !== void 0 && storyData.length)) return;
    (_storyData$current = storyData[current]) === null || _storyData$current === void 0 || _storyData$current.analytics.markAsSeen();
  }, [current, storyData]);
  const close = (0, _react.useCallback)(state => {
    onFinish && onFinish(state, targetId);
  }, [onFinish, targetId]);
  const onClose = (0, _react.useCallback)(() => {
    onFinish && onFinish();
  }, [onFinish]);
  if (!targetId) return null;
  if (loading || !currentStory || !communityData || !storyData) {
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.spinnerContainer
    }, /*#__PURE__*/React.createElement(_reactNative.ActivityIndicator, {
      animating: loading,
      size: "large",
      color: 'white'
    }));
  }
  if (currentPage === index) {
    return /*#__PURE__*/React.createElement(_AmityViewStoryItem.default, {
      key: storyData[current].storyId,
      communityData: communityData,
      communityAvatar: communityAvatar,
      storyData: storyData,
      hasStoryImpressionPermission: hasStoryImpressionPermission,
      close: close,
      onClose: onClose,
      setCurrent: setCurrent,
      current: current,
      hasStoryPermission: hasStoryPermission,
      onPressAvatar: onPressAvatar,
      onPressCommunityName: onPressCommunityName,
      onNavigateToUser: onNavigateToUser
    });
  }
  return null;
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(AmityViewStoryPage);
//# sourceMappingURL=View.js.map