"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("react-native");
var _vectorIcons = require("@expo/vector-icons");
var _Avatar = _interopRequireDefault(require("../../components/Avatar"));
var _GroupedPillButtons = _interopRequireDefault(require("../../components/internal/GroupedPillButtons"));
var _TextField = _interopRequireDefault(require("../../components/internal/TextField"));
var _useI18n = require("../../hooks/useI18n");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const SignInPasswordStep = ({
  colors,
  styles,
  theme,
  navigate,
  nextStep,
  prevStep,
  password,
  setPassword,
  showPassword,
  setShowPassword,
  errorMessage,
  setErrorMessage,
  isLoading,
  userProfile,
  username,
  handleSignIn,
  mfaToken
}) => {
  const inputRef = (0, _react.useRef)(null);
  const {
    t
  } = (0, _useI18n.useI18n)();
  const handlePasswordChange = text => {
    setPassword(text);
    if (errorMessage) setErrorMessage('');
  };
  const handleSignInSubmit = async () => {
    if (!password) {
      setErrorMessage('Please enter your password.');
      setTimeout(() => inputRef.current?.focus(), 0);
      return;
    }

    // Call the actual sign-in function passed from props
    await handleSignIn();
  };
  const togglePasswordVisibility = () => {
    setShowPassword(!showPassword);
  };

  // Focus password input on error
  (0, _react.useEffect)(() => {
    if (errorMessage) {
      setTimeout(() => {
        inputRef.current?.focus();
      }, 0);
    }
  }, [errorMessage]);

  // Auto-advance when MFA is required
  (0, _react.useEffect)(() => {
    if (mfaToken) {
      // Move to TOTP step when token is available
      nextStep();
    }
  }, [mfaToken, nextStep]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: styles.modernUserProfileContainer,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: styles.avatarContainer,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Avatar.default, {
          name: userProfile?.displayName || userProfile?.name || username,
          size: 100,
          theme: theme,
          style: styles.modernUserAvatar,
          backgroundColor: colors.primary + '20'
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
          style: [styles.statusIndicator, {
            backgroundColor: colors.primary
          }]
        })]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
        style: [styles.modernUserDisplayName, {
          color: colors.text
        }],
        children: userProfile?.displayName || userProfile?.name || username
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.Text, {
        style: [styles.modernUsernameSubtext, {
          color: colors.secondaryText
        }],
        children: ["@", username]
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: styles.modernInputContainer,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_TextField.default, {
        ref: inputRef,
        label: t('common.labels.password'),
        leading: /*#__PURE__*/(0, _jsxRuntime.jsx)(_vectorIcons.Ionicons, {
          name: "lock-closed-outline",
          size: 24,
          color: colors.secondaryText
        }),
        value: password,
        onChangeText: handlePasswordChange,
        secureTextEntry: !showPassword,
        autoCapitalize: "none",
        autoCorrect: false,
        testID: "password-input",
        variant: "filled",
        error: errorMessage || undefined,
        onSubmitEditing: handleSignInSubmit,
        autoFocus: true
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
        style: {
          flexDirection: 'row',
          alignItems: 'center',
          marginBottom: 16
        },
        children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.Text, {
          style: [styles.footerText, {
            color: colors.text
          }],
          children: [t('signin.forgotPrompt') || 'Forgot your password?', " "]
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableOpacity, {
          onPress: () => navigate('RecoverAccount', {
            returnTo: 'SignIn',
            returnStep: 1,
            returnData: {
              username,
              userProfile
            }
          }),
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
            style: [styles.modernLinkText, {
              color: colors.primary
            }],
            children: t('common.links.recoverAccount')
          })
        })]
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_GroupedPillButtons.default, {
      buttons: [{
        text: t('common.actions.back') || 'Back',
        onPress: prevStep,
        icon: 'arrow-back',
        variant: 'transparent'
      }, {
        text: t('common.actions.signIn') || 'Sign In',
        onPress: handleSignInSubmit,
        icon: 'log-in',
        variant: 'primary',
        loading: isLoading,
        testID: 'login-button'
      }],
      colors: colors
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
      style: styles.securityNotice,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_vectorIcons.Ionicons, {
        name: "shield-checkmark",
        size: 14,
        color: colors.secondaryText
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
        style: [styles.securityText, {
          color: colors.secondaryText
        }],
        children: t('signin.security.dataSecure') || 'Your data is encrypted and secure'
      })]
    })]
  });
};
var _default = exports.default = SignInPasswordStep;
//# sourceMappingURL=SignInPasswordStep.js.map