"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _reactHooks = require("@credo-ts/react-hooks");
var _react = _interopRequireWildcard(require("react"));
var _reactI18next = require("react-i18next");
var _reactNative = require("react-native");
var _reactNativeSafeAreaContext = require("react-native-safe-area-context");
var _Button = _interopRequireWildcard(require("../components/buttons/Button"));
var _ThemedText = require("../components/texts/ThemedText");
var _PushNotificationsContent = _interopRequireDefault(require("../components/views/PushNotificationsContent"));
var _PushNotificationsDisabledContent = _interopRequireDefault(require("../components/views/PushNotificationsDisabledContent"));
var _containerApi = require("../container-api");
var _store = require("../contexts/reducers/store");
var _store2 = require("../contexts/store");
var _theme = require("../contexts/theme");
var _testable = require("../utils/testable");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
// Screen accesible through settings that allows the user to
// enable or disable push notifications
const TogglePushNotifications = () => {
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const [store, dispatch] = (0, _store2.useStore)();
  const {
    agent
  } = (0, _reactHooks.useAgent)();
  const {
    ColorPallet
  } = (0, _theme.useTheme)();
  const [{
    enablePushNotifications
  }] = (0, _containerApi.useServices)([_containerApi.TOKENS.CONFIG]);
  const [notificationState, setNotificationState] = (0, _react.useState)(store.preferences.usePushNotifications);
  const [notificationStatus, setNotificationStatus] = (0, _react.useState)('unknown');
  if (!enablePushNotifications) {
    throw new Error('Push notification configuration not found');
  }
  const styles = _reactNative.StyleSheet.create({
    screenContainer: {
      flex: 1,
      padding: 30
    },
    toggleContainer: {
      marginTop: 25,
      display: 'flex',
      flexDirection: 'row',
      justifyContent: 'space-between'
    },
    controlsContainer: {
      marginTop: 'auto'
    }
  });
  (0, _react.useEffect)(() => {
    const updateNotificationState = async () => {
      const status = await enablePushNotifications.status();
      setNotificationStatus(status);
    };
    updateNotificationState();
    const subscription = _reactNative.AppState.addEventListener('change', updateNotificationState);
    return () => subscription.remove();
  }, [enablePushNotifications]);
  const hasNotificationsDisabled = notificationStatus === 'denied' && store.onboarding.didConsiderPushNotifications;
  const toggleSwitch = async () => {
    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: styles.screenContainer
  }, hasNotificationsDisabled ? /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_PushNotificationsDisabledContent.default, null), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.controlsContainer
  }, /*#__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(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_PushNotificationsContent.default, null), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.toggleContainer
  }, /*#__PURE__*/_react.default.createElement(_ThemedText.ThemedText, null, 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
  }))))));
};
var _default = exports.default = TogglePushNotifications;
//# sourceMappingURL=TogglePushNotifications.js.map