"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _native = require("@react-navigation/native");
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _NotificationListItem = _interopRequireWildcard(require("../components/listItems/NotificationListItem"));
var _NoNewUpdates = _interopRequireDefault(require("../components/misc/NoNewUpdates"));
var _AppGuideModal = _interopRequireDefault(require("../components/modals/AppGuideModal"));
var _configuration = require("../contexts/configuration");
var _store = require("../contexts/reducers/store");
var _store2 = require("../contexts/store");
var _theme = require("../contexts/theme");
var _tourContext = require("../contexts/tour/tour-context");
var _tour = require("../types/tour");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 Home = () => {
  const {
    useCustomNotifications,
    enableTours: enableToursConfig,
    homeFooterView: HomeFooterView,
    homeHeaderView: HomeHeaderView
  } = (0, _configuration.useConfiguration)();
  const {
    notifications
  } = useCustomNotifications();
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    ColorPallet
  } = (0, _theme.useTheme)();
  const [store, dispatch] = (0, _store2.useStore)();
  const {
    start
  } = (0, _tourContext.useTour)();
  const [showTourPopup, setShowTourPopup] = (0, _react.useState)(false);
  const screenIsFocused = (0, _native.useIsFocused)();
  const styles = _reactNative.StyleSheet.create({
    flatlist: {
      marginBottom: 35
    },
    noNewUpdatesContainer: {
      paddingHorizontal: 20,
      paddingVertical: 20,
      backgroundColor: ColorPallet.brand.secondaryBackground
    }
  });
  const DisplayListItemType = item => {
    let component;
    if (item.type === 'BasicMessageRecord') {
      component = /*#__PURE__*/_react.default.createElement(_NotificationListItem.default, {
        notificationType: _NotificationListItem.NotificationType.BasicMessage,
        notification: item
      });
    } else if (item.type === 'CredentialRecord') {
      let notificationType = _NotificationListItem.NotificationType.CredentialOffer;
      if (item.revocationNotification) {
        notificationType = _NotificationListItem.NotificationType.Revocation;
      }
      component = /*#__PURE__*/_react.default.createElement(_NotificationListItem.default, {
        notificationType: notificationType,
        notification: item
      });
    } else if (item.type === 'CustomNotification') {
      component = /*#__PURE__*/_react.default.createElement(_NotificationListItem.default, {
        notificationType: _NotificationListItem.NotificationType.Custom,
        notification: item
      });
    } else {
      component = /*#__PURE__*/_react.default.createElement(_NotificationListItem.default, {
        notificationType: _NotificationListItem.NotificationType.ProofRequest,
        notification: item
      });
    }
    return component;
  };
  (0, _react.useEffect)(() => {
    const shouldShowTour = enableToursConfig && store.tours.enableTours && !store.tours.seenHomeTour;
    if (shouldShowTour && screenIsFocused) {
      if (store.tours.seenToursPrompt) {
        dispatch({
          type: _store.DispatchAction.UPDATE_SEEN_HOME_TOUR,
          payload: [true]
        });
        start(_tour.TourID.HomeTour);
      } else {
        dispatch({
          type: _store.DispatchAction.UPDATE_SEEN_TOUR_PROMPT,
          payload: [true]
        });
        setShowTourPopup(true);
      }
    }
  }, [screenIsFocused]);
  const onCTAPressed = () => {
    setShowTourPopup(false);
    dispatch({
      type: _store.DispatchAction.ENABLE_TOURS,
      payload: [true]
    });
    dispatch({
      type: _store.DispatchAction.UPDATE_SEEN_HOME_TOUR,
      payload: [true]
    });
    start(_tour.TourID.HomeTour);
  };
  const onDismissPressed = () => {
    setShowTourPopup(false);
    dispatch({
      type: _store.DispatchAction.ENABLE_TOURS,
      payload: [false]
    });
  };
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactNative.FlatList, {
    style: styles.flatlist,
    showsVerticalScrollIndicator: false,
    scrollEnabled: (notifications === null || notifications === void 0 ? void 0 : notifications.length) > 0 ? true : false,
    decelerationRate: "fast",
    ListEmptyComponent: () => /*#__PURE__*/_react.default.createElement(_reactNative.View, {
      style: styles.noNewUpdatesContainer
    }, /*#__PURE__*/_react.default.createElement(_NoNewUpdates.default, null)),
    ListHeaderComponent: () => /*#__PURE__*/_react.default.createElement(HomeHeaderView, null),
    ListFooterComponent: () => /*#__PURE__*/_react.default.createElement(HomeFooterView, null),
    data: notifications,
    keyExtractor: item => item.id,
    renderItem: _ref => {
      let {
        item,
        index
      } = _ref;
      return /*#__PURE__*/_react.default.createElement(_reactNative.View, {
        style: {
          paddingHorizontal: 20,
          paddingTop: index === 0 ? 20 : 0,
          paddingBottom: index === notifications.length - 1 ? 20 : 10,
          backgroundColor: ColorPallet.brand.secondaryBackground
        }
      }, DisplayListItemType(item));
    }
  }), showTourPopup && /*#__PURE__*/_react.default.createElement(_AppGuideModal.default, {
    title: t('Tour.GuideTitle'),
    description: t('Tour.WouldYouLike'),
    onCallToActionPressed: onCTAPressed,
    onCallToActionLabel: t('Tour.UseAppGuides'),
    onSecondCallToActionPressed: onDismissPressed,
    onSecondCallToActionLabel: t('Tour.DoNotUseAppGuides'),
    onDismissPressed: onDismissPressed
  }));
};
var _default = Home;
exports.default = _default;
//# sourceMappingURL=Home.js.map