"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactNative = require("react-native");
var _react = require("react");
var _AndroidCubeEffect = _interopRequireDefault(require("../StoryKit/src/components/AndroidCubeEffect"));
var _CubeNavigationHorizontal = _interopRequireDefault(require("../StoryKit/src/components/CubeNavigationHorizontal"));
var _View = _interopRequireDefault(require("../../features/story/View/View"));
var _native = require("@react-navigation/native");
var _hooks = require("../../hooks");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const StoryTargetView = ({
  currentCommunityIndex,
  setCurrentCommunityIndex,
  globalStoryTargets,
  setViewStory,
  onClose
}) => {
  var _cube$current;
  const cube = (0, _react.useRef)(null);
  const communityId = globalStoryTargets[currentCommunityIndex].targetId;
  const {
    hasStoryPermission
  } = (0, _hooks.useStoryPermission)(communityId);
  const navigation = (0, _native.useNavigation)();
  (0, _react.useLayoutEffect)(() => {
    cube && cube.current.scrollTo(currentCommunityIndex);
  }, [currentCommunityIndex]);
  cube && (cube === null || cube === void 0 || (_cube$current = cube.current) === null || _cube$current === void 0 ? void 0 : _cube$current.scrollTo(currentCommunityIndex));
  const onPressCreateStory = (0, _react.useCallback)(() => {
    hasStoryPermission && navigation.navigate('CreateStory', {
      targetId: communityId,
      targetType: 'community'
    });
  }, [communityId, hasStoryPermission, navigation]);
  const onPressAvatar = (0, _react.useCallback)(() => {
    setViewStory(false);
    onPressCreateStory();
  }, [onPressCreateStory, setViewStory]);
  const onPressCommunityName = (0, _react.useCallback)(() => {
    setViewStory(false);
    navigation.navigate('CommunityProfilePage', {
      communityId: communityId
    });
  }, [communityId, navigation, setViewStory]);
  const onFinish = (0, _react.useCallback)(state => {
    if (state === 'next') {
      if (currentCommunityIndex < globalStoryTargets.length - 1) {
        return setCurrentCommunityIndex(currentCommunityIndex + 1);
      }
      setViewStory(false);
      onClose && onClose();
      return;
    }
    if (state === 'previous') {
      if (currentCommunityIndex > 0) return setCurrentCommunityIndex(currentCommunityIndex - 1);
    }
    setViewStory(false);
    onClose && onClose();
  }, [currentCommunityIndex, globalStoryTargets.length, onClose, setCurrentCommunityIndex, setViewStory]);
  const handleNavigateToUser = (0, _react.useCallback)(userId => {
    setViewStory(false);
    navigation.navigate('UserProfile', {
      userId
    });
    const unsubscribe = navigation.addListener('focus', () => {
      unsubscribe();
      setViewStory(true);
    });
  }, [navigation, setViewStory]);
  if (_reactNative.Platform.OS === 'ios') {
    return /*#__PURE__*/React.createElement(_CubeNavigationHorizontal.default, {
      ref: cube,
      callBackAfterSwipe: x => {
        if (x !== currentCommunityIndex) {
          setCurrentCommunityIndex(parseInt(x, 10));
        }
      }
    }, globalStoryTargets.map((storyTarget, i) => {
      return /*#__PURE__*/React.createElement(_View.default, {
        targetType: 'community',
        targetId: storyTarget.targetId,
        key: storyTarget.targetId,
        onFinish: onFinish,
        onPressAvatar: onPressAvatar,
        onPressCommunityName: onPressCommunityName,
        onNavigateToUser: handleNavigateToUser,
        index: i,
        currentPage: currentCommunityIndex
      });
    }));
  } else {
    return /*#__PURE__*/React.createElement(_AndroidCubeEffect.default, {
      ref: cube,
      callBackAfterSwipe: x => {
        if (x !== currentCommunityIndex) {
          setCurrentCommunityIndex(parseInt(x, 10));
        }
      }
    }, globalStoryTargets.map((storyTarget, i) => {
      return /*#__PURE__*/React.createElement(_View.default, {
        targetType: 'community',
        targetId: storyTarget.targetId,
        key: storyTarget.targetId,
        onFinish: onFinish,
        onPressAvatar: onPressAvatar,
        onPressCommunityName: onPressCommunityName,
        onNavigateToUser: handleNavigateToUser,
        index: i,
        currentPage: currentCommunityIndex
      });
    }));
  }
};
var _default = exports.default = StoryTargetView;
//# sourceMappingURL=StoryTargetView.js.map