"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.AmityGlobalStoryTabWrapper = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _styles = require("./styles");
var _useGlobalStory = require("../../hooks/useGlobalStory");
var _native = _interopRequireWildcard(require("react-content-loader/native"));
var _StoryCircleItem = _interopRequireDefault(require("./StoryCircleItem"));
var _reactNativeModalbox = _interopRequireDefault(require("react-native-modalbox"));
var _StoryTargetView = _interopRequireDefault(require("./StoryTargetView"));
var _reactNativePaper = require("react-native-paper");
var _useAuth = _interopRequireDefault(require("../../../core/hooks/useAuth"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const MyStories = () => {
  const styles = (0, _styles.useStyles)();
  const theme = (0, _reactNativePaper.useTheme)();
  const {
    getGlobalStoryTargets,
    globalStoryTargets,
    loading,
    globalStoryTargetsOnNextPage
  } = (0, _useGlobalStory.useGlobalStory)();
  const [currentCommunityIndex, setCurrentCommunityIndex] = (0, _react.useState)(0);
  const [viewStory, setViewStory] = (0, _react.useState)(false);
  (0, _react.useEffect)(() => {
    getGlobalStoryTargets();
  }, [getGlobalStoryTargets]);
  const onPressStoryView = (0, _react.useCallback)(storyTarget => {
    setViewStory(true);
    const currentIndex = globalStoryTargets.findIndex(target => target.targetId === storyTarget.targetId);
    setCurrentCommunityIndex(currentIndex);
  }, [globalStoryTargets]);
  if (loading && !globalStoryTargets) {
    return /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.skeletonContainer
    }, Array.from({
      length: 6
    }, (_, index) => {
      return (
        /*#__PURE__*/
        // eslint-disable-next-line react-native/no-inline-styles
        React.createElement(_reactNative.View, {
          style: {
            padding: 10
          },
          key: index
        }, /*#__PURE__*/React.createElement(_native.default, {
          height: 70,
          speed: 1,
          width: 70,
          backgroundColor: theme.colors.baseShade4,
          foregroundColor: theme.colors.baseShade2,
          viewBox: "0 0 50 50"
        }, /*#__PURE__*/React.createElement(_native.Circle, {
          cx: "25",
          cy: "25",
          r: "25"
        })))
      );
    }));
  }
  if (globalStoryTargets && globalStoryTargets.length > 0) {
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_reactNative.View, {
      style: styles.container
    }, /*#__PURE__*/React.createElement(_reactNative.FlatList, {
      showsHorizontalScrollIndicator: false,
      onEndReached: globalStoryTargetsOnNextPage,
      contentContainerStyle: styles.scrollContainer,
      horizontal: true,
      data: globalStoryTargets,
      renderItem: ({
        item: storyTarget
      }) => {
        return /*#__PURE__*/React.createElement(_StoryCircleItem.default, {
          onPressStoryView: onPressStoryView,
          storyTarget: storyTarget
        });
      },
      keyExtractor: item => item.targetId
    })), /*#__PURE__*/React.createElement(_reactNativeModalbox.default, {
      style: styles.modal,
      isOpen: viewStory,
      onClosed: () => setViewStory(false),
      position: "center",
      swipeToClose: true,
      swipeArea: 250,
      backButtonClose: true,
      coverScreen: true,
      animationDuration: 600
    }, /*#__PURE__*/React.createElement(_StoryTargetView.default, {
      currentCommunityIndex: currentCommunityIndex,
      setCurrentCommunityIndex: setCurrentCommunityIndex,
      globalStoryTargets: globalStoryTargets,
      setViewStory: setViewStory,
      onClose: getGlobalStoryTargets
    })));
  }
  return null;
};
const AmityGlobalStoryTabWrapper = ({
  children
}) => {
  const {
    globalStoryTargets,
    getGlobalStoryTargets
  } = (0, _useGlobalStory.useGlobalStory)();
  const {
    sessionState
  } = (0, _useAuth.default)();
  (0, _react.useEffect)(() => {
    if (sessionState === 'established') {
      getGlobalStoryTargets();
    }
  }, [sessionState, getGlobalStoryTargets]);
  return /*#__PURE__*/React.createElement(_reactNative.View, {
    style: {
      height: globalStoryTargets.length > 0 ? 113 : 0
    }
  }, children);
};
exports.AmityGlobalStoryTabWrapper = AmityGlobalStoryTabWrapper;
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(MyStories);
//# sourceMappingURL=index.js.map