"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _core = require("@react-navigation/core");
var _native = require("@react-navigation/native");
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _HeaderButton = _interopRequireWildcard(require("../components/buttons/HeaderButton"));
var _Pagination = require("../components/misc/Pagination");
var _store = require("../contexts/reducers/store");
var _store2 = require("../contexts/store");
var _navigators = require("../types/navigators");
var _testable = require("../utils/testable");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const Onboarding = _ref => {
  let {
    pages,
    nextButtonText,
    previousButtonText,
    style,
    disableSkip = false
  } = _ref;
  const [activeIndex, setActiveIndex] = (0, _react.useState)(0);
  const flatList = (0, _react.useRef)(null);
  const scrollX = (0, _react.useRef)(new _reactNative.Animated.Value(0)).current;
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const navigation = (0, _core.useNavigation)();
  const [, dispatch] = (0, _store2.useStore)();
  const {
    width
  } = (0, _reactNative.useWindowDimensions)();
  const onViewableItemsChangedRef = (0, _react.useRef)(_ref2 => {
    let {
      viewableItems
    } = _ref2;
    if (!viewableItems[0]) {
      return;
    }
    setActiveIndex(viewableItems[0].index);
  });
  const viewabilityConfigRef = (0, _react.useRef)({
    viewAreaCoveragePercentThreshold: 60
  });
  const onScroll = _reactNative.Animated.event([{
    nativeEvent: {
      contentOffset: {
        x: scrollX
      }
    }
  }], {
    useNativeDriver: false
  });
  const next = () => {
    if (activeIndex + 1 < pages.length) {
      var _flatList$current;
      flatList === null || flatList === void 0 ? void 0 : (_flatList$current = flatList.current) === null || _flatList$current === void 0 ? void 0 : _flatList$current.scrollToIndex({
        index: activeIndex + 1,
        animated: true
      });
    }
  };
  const previous = () => {
    if (activeIndex !== 0) {
      var _flatList$current2;
      flatList === null || flatList === void 0 ? void 0 : (_flatList$current2 = flatList.current) === null || _flatList$current2 === void 0 ? void 0 : _flatList$current2.scrollToIndex({
        index: activeIndex - 1,
        animated: true
      });
    }
  };
  const renderItem = (0, _react.useCallback)(_ref3 => {
    let {
      item,
      index
    } = _ref3;
    return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      key: index,
      style: [{
        width
      }, style.carouselContainer]
    }, item);
  }, []);
  const onSkipTouched = () => {
    dispatch({
      type: _store.DispatchAction.DID_COMPLETE_TUTORIAL
    });
    navigation.navigate(_navigators.Screens.Terms);
  };
  (0, _react.useEffect)(() => {
    !disableSkip && navigation.setOptions({
      headerRight: () => /*#__PURE__*/_react.default.createElement(_HeaderButton.default, {
        buttonLocation: _HeaderButton.ButtonLocation.Right,
        accessibilityLabel: t('Onboarding.SkipA11y'),
        testID: (0, _testable.testIdWithKey)('Skip'),
        onPress: onSkipTouched,
        icon: "chevron-right",
        text: t('Global.Skip')
      })
    });
    if (!disableSkip && activeIndex + 1 === pages.length) {
      navigation.setOptions({
        headerRight: () => false
      });
    }
  }, [activeIndex]);
  (0, _native.useFocusEffect)((0, _react.useCallback)(() => {
    const onBackPress = () => {
      _reactNative.BackHandler.exitApp();
      return true;
    };
    _reactNative.BackHandler.addEventListener('hardwareBackPress', onBackPress);
    return () => _reactNative.BackHandler.removeEventListener('hardwareBackPress', onBackPress);
  }, []));
  return /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaView, {
    style: style.container,
    edges: ['left', 'right', 'bottom']
  }, /*#__PURE__*/_react.default.createElement(_reactNative.FlatList, {
    ref: flatList,
    horizontal: true,
    pagingEnabled: true,
    showsHorizontalScrollIndicator: false,
    style: [{
      width
    }],
    data: pages,
    renderItem: renderItem,
    viewabilityConfig: viewabilityConfigRef.current,
    onViewableItemsChanged: onViewableItemsChangedRef.current,
    onScroll: onScroll,
    scrollEventThrottle: 16
  }), /*#__PURE__*/_react.default.createElement(_Pagination.Pagination, {
    pages: pages,
    activeIndex: activeIndex,
    nextButtonText: nextButtonText,
    previousButtonText: previousButtonText,
    scrollX: scrollX,
    style: style,
    next: next,
    previous: previous
  }));
};
var _default = Onboarding;
exports.default = _default;
//# sourceMappingURL=Onboarding.js.map