"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 _ButtonLoading = _interopRequireDefault(require("../components/animated/ButtonLoading"));
var _Button = _interopRequireWildcard(require("../components/buttons/Button"));
var _LimitedTextInput = _interopRequireDefault(require("../components/inputs/LimitedTextInput"));
var _InfoBox = require("../components/misc/InfoBox");
var _PopupModal = _interopRequireDefault(require("../components/modals/PopupModal"));
var _KeyboardView = _interopRequireDefault(require("../components/views/KeyboardView"));
var _store = require("../contexts/reducers/store");
var _store2 = require("../contexts/store");
var _theme = require("../contexts/theme");
var _navigators = require("../types/navigators");
var _helpers = require("../utils/helpers");
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; }
const NameWallet = () => {
  const {
    t
  } = (0, _reactI18next.useTranslation)();
  const {
    ColorPallet,
    TextTheme,
    Assets
  } = (0, _theme.useTheme)();
  const navigation = (0, _core.useNavigation)();
  const [store, dispatch] = (0, _store2.useStore)();
  const {
    agent
  } = (0, _reactHooks.useAgent)();
  const [walletName, setWalletName] = (0, _react.useState)(store.preferences.walletName ?? (0, _helpers.generateRandomWalletName)());
  const [loading, setLoading] = (0, _react.useState)(false);
  const onBoardingComplete = store.onboarding.didCompleteTutorial && store.onboarding.didAgreeToTerms && store.onboarding.didCreatePIN && store.onboarding.didConsiderBiometry;
  const [errorState, setErrorState] = (0, _react.useState)({
    visible: false,
    title: '',
    description: ''
  });
  const styles = _reactNative.StyleSheet.create({
    screenContainer: {
      height: '100%',
      backgroundColor: ColorPallet.brand.primaryBackground,
      padding: 20,
      justifyContent: 'space-between'
    },
    contentContainer: {
      justifyContent: 'center',
      alignItems: 'center',
      width: '100%'
    },
    // below used as helpful label for view, no properties needed atp
    controlsContainer: {},
    buttonContainer: {
      width: '100%'
    }
  });
  const handleChangeText = text => {
    setWalletName(text);
  };
  const handleCancelPressed = () => {
    navigation.goBack();
  };
  const handleContinuePressed = () => {
    if (walletName.length < 1) {
      setErrorState({
        title: t('NameWallet.EmptyNameTitle'),
        description: t('NameWallet.EmptyNameDescription'),
        visible: true
      });
    } else if (walletName.length > 50) {
      setErrorState({
        title: t('NameWallet.CharCountTitle'),
        description: t('NameWallet.CharCountDescription'),
        visible: true
      });
    } else {
      setLoading(true);
      dispatch({
        type: _store.DispatchAction.UPDATE_WALLET_NAME,
        payload: [walletName]
      });
      if (agent) {
        agent.config.label = walletName;
      }
      dispatch({
        type: _store.DispatchAction.DID_NAME_WALLET
      });
      if (onBoardingComplete) {
        navigation.goBack();
      } else {
        navigation.navigate({
          name: _navigators.Screens.UseBiometry
        });
      }
    }
  };
  const handleDismissError = () => {
    setErrorState(prev => ({
      ...prev,
      visible: false
    }));
  };
  return /*#__PURE__*/_react.default.createElement(_KeyboardView.default, null, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.screenContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.contentContainer
  }, /*#__PURE__*/_react.default.createElement(Assets.svg.contactBook, {
    height: 100,
    style: {
      marginVertical: 20
    }
  }), /*#__PURE__*/_react.default.createElement(_reactNative.Text, {
    style: [TextTheme.normal, {
      width: '100%',
      marginBottom: 16
    }]
  }, t('NameWallet.ThisIsTheName')), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      width: '100%'
    }
  }, /*#__PURE__*/_react.default.createElement(_LimitedTextInput.default, {
    defaultValue: walletName,
    label: t('NameWallet.NameYourWallet'),
    limit: 50,
    handleChangeText: handleChangeText,
    accessibilityLabel: t('NameWallet.NameYourWallet'),
    testID: (0, _testable.testIdWithKey)('NameInput')
  }))), /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.controlsContainer
  }, /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: styles.buttonContainer
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    title: onBoardingComplete ? t('Global.Save') : t('Global.Continue'),
    buttonType: _Button.ButtonType.Primary,
    testID: onBoardingComplete ? (0, _testable.testIdWithKey)('Save') : (0, _testable.testIdWithKey)('Continue'),
    accessibilityLabel: onBoardingComplete ? t('Global.Save') : t('Global.Continue'),
    onPress: handleContinuePressed,
    disabled: loading
  }, loading && /*#__PURE__*/_react.default.createElement(_ButtonLoading.default, null)), onBoardingComplete && /*#__PURE__*/_react.default.createElement(_reactNative.View, {
    style: {
      marginTop: 15
    }
  }, /*#__PURE__*/_react.default.createElement(_Button.default, {
    title: t('Global.Cancel'),
    buttonType: _Button.ButtonType.Secondary,
    testID: (0, _testable.testIdWithKey)('Cancel'),
    accessibilityLabel: t('Global.Cancel'),
    onPress: handleCancelPressed
  }))))), errorState.visible && /*#__PURE__*/_react.default.createElement(_PopupModal.default, {
    notificationType: _InfoBox.InfoBoxType.Info,
    onCallToActionLabel: t('Global.Okay'),
    onCallToActionPressed: handleDismissError,
    title: errorState.title,
    description: errorState.description
  }));
};
var _default = NameWallet;
exports.default = _default;
//# sourceMappingURL=NameWallet.js.map