"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactHooks = require("@aries-framework/react-hooks");
var _core = require("@react-navigation/core");
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _pushNotifications = _interopRequireDefault(require("../assets/img/push-notifications.svg"));
var _Button = _interopRequireWildcard(require("../components/buttons/Button"));
var _configuration = require("../contexts/configuration");
var _store = require("../contexts/reducers/store");
var _store2 = require("../contexts/store");
var _theme = require("../contexts/theme");
var _navigators = require("../types/navigators");
var _testable = require("../utils/testable");
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; }
// import { setup } from '../utils/PushNotificationsHelper'

const PushNotification = _ref => {
  var _route$params;
  let {
    route
  } = _ref;
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const [store, dispatch] = (0, _store2.useStore)();
  const {
    agent
  } = (0, _reactHooks.useAgent)();
  const {
    TextTheme,
    ColorPallet
  } = (0, _theme.useTheme)();
  const {
    enablePushNotifications
  } = (0, _configuration.useConfiguration)();
  const [notificationState, setNotificationState] = (0, _react.useState)(store.preferences.usePushNotifications);
  const [notificationStatus, setNotificationStatus] = (0, _react.useState)('unknown');
  const navigation = (0, _core.useNavigation)();
  if (!enablePushNotifications) {
    throw new Error('Push notification configuration not found');
  }
  const isMenu = (_route$params = route.params) === null || _route$params === void 0 ? void 0 : _route$params.isMenu;
  const updateNotificationState = async () => {
    const status = await enablePushNotifications.status();
    setNotificationStatus(status);
  };
  (0, _react.useMemo)(() => {
    updateNotificationState();
    _reactNative.AppState.addEventListener('change', updateNotificationState);
  }, []);
  const style = _reactNative.StyleSheet.create({
    screenContainer: {
      flex: 1,
      padding: 30
    },
    image: {
      height: 200,
      marginBottom: 20
    },
    heading: {
      marginBottom: 20
    },
    listItem: {
      ...TextTheme.normal,
      flex: 1,
      paddingLeft: 5
    }
  });
  const list = [t('PushNotifications.BulletOne'), t('PushNotifications.BulletTwo'), t('PushNotifications.BulletThree'), t('PushNotifications.BulletFour')];
  const settingsInstructions = [t('PushNotifications.InstructionsOne'), t('PushNotifications.InstructionsTwo'), t('PushNotifications.InstructionsThree')];
  const hasNotificationsDisabled = notificationStatus === 'denied' && store.onboarding.didConsiderPushNotifications;
  const activatePushNotifications = async () => {
    const state = await enablePushNotifications.setup();
    dispatch({
      type: _store.DispatchAction.USE_PUSH_NOTIFICATIONS,
      payload: [state === 'granted']
    });
    if (store.preferences.enableWalletNaming) {
      navigation.dispatch(_core.CommonActions.reset({
        index: 0,
        routes: [{
          name: _navigators.Screens.NameWallet
        }]
      }));
    }
  };
  const toggleSwitch = async () => {
    if (agent) {
      if (!notificationState) {
        const res = await enablePushNotifications.setup();
        if (res === 'denied') {
          return;
        }
      }
      dispatch({
        type: _store.DispatchAction.USE_PUSH_NOTIFICATIONS,
        payload: [!notificationState]
      });
      enablePushNotifications.toggle(!notificationState, agent);
      setNotificationState(!notificationState);
    }
  };
  return /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaView, {
    style: {
      flex: 1
    },
    edges: ['left', 'right', 'bottom']
  }, /*#__PURE__*/_react.default.createElement(_reactNative.ScrollView, {
    contentContainerStyle: {
      flexGrow: 1
    }
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.screenContainer
  }, !hasNotificationsDisabled && /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: style.image
  }, /*#__PURE__*/_react.default.createElement(_pushNotifications.default, null)), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.headingThree, style.heading]
  }, t('PushNotifications.EnableNotifiactions')), hasNotificationsDisabled ? /*#__PURE__*/_react.default.createElement(_reactNative.View, null, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: TextTheme.normal
  }, t('PushNotifications.NotificationsOffMessage'))) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.normal]
  }, t('PushNotifications.BeNotified')), list.map((item, index) => /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      flexDirection: 'row',
      marginTop: 20
    },
    key: index
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: TextTheme.normal
  }, '\u2022'), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: style.listItem
  }, item)))), isMenu ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      marginTop: 25
    }
  }, hasNotificationsDisabled ? /*#__PURE__*/_react.default.createElement(_reactNative.View, null, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: TextTheme.bold
  }, t('PushNotifications.NotificationsOffTitle')), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: TextTheme.normal
  }, t('PushNotifications.NotificationsInstructionTitle')), settingsInstructions.map((item, index) => /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      flexDirection: 'row',
      marginTop: 20
    },
    key: index
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: TextTheme.normal
  }, `${index + 1}. `), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: style.listItem
  }, item)))) : /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      display: 'flex',
      flexDirection: 'row',
      justifyContent: 'space-between'
    }
  }, /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: TextTheme.normal
  }, t('PushNotifications.ReceiveNotifications')), /*#__PURE__*/_react.default.createElement(_reactNative.Switch, {
    trackColor: {
      false: ColorPallet.grayscale.lightGrey,
      true: ColorPallet.brand.primaryDisabled
    },
    thumbColor: notificationState ? ColorPallet.brand.primary : ColorPallet.grayscale.mediumGrey,
    ios_backgroundColor: ColorPallet.grayscale.lightGrey,
    onValueChange: toggleSwitch,
    accessibilityLabel: t('PushNotifications.ReceiveNotifications'),
    accessibilityRole: "switch",
    testID: (0, _testable.testIdWithKey)('PushNotificationSwitch'),
    value: notificationState
  }))), hasNotificationsDisabled && /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      marginTop: 'auto'
    }
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    buttonType: _Button.ButtonType.Primary,
    title: t('PushNotifications.OpenSettings'),
    accessibilityLabel: t('PushNotifications.OpenSettings'),
    testID: (0, _testable.testIdWithKey)('PushNotificationSettings'),
    onPress: () => _reactNative.Linking.openSettings()
  }))) : /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      marginTop: 'auto'
    }
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    buttonType: _Button.ButtonType.Primary,
    title: t('Global.Continue'),
    accessibilityLabel: t('Global.Continue'),
    testID: (0, _testable.testIdWithKey)('PushNotificationContinue'),
    onPress: activatePushNotifications
  })))));
};
var _default = PushNotification;
exports.default = _default;
//# sourceMappingURL=PushNotification.js.map