"use strict";

import { useRef, useEffect } from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import Avatar from '../../components/Avatar';
import GroupedPillButtons from '../../components/internal/GroupedPillButtons';
import TextField from '../../components/internal/TextField';
import { useI18n } from '../../hooks/useI18n';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
const SignInPasswordStep = ({
  colors,
  styles,
  theme,
  navigate,
  nextStep,
  prevStep,
  password,
  setPassword,
  showPassword,
  setShowPassword,
  errorMessage,
  setErrorMessage,
  isLoading,
  userProfile,
  username,
  handleSignIn,
  mfaToken
}) => {
  const inputRef = useRef(null);
  const {
    t
  } = 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
  useEffect(() => {
    if (errorMessage) {
      setTimeout(() => {
        inputRef.current?.focus();
      }, 0);
    }
  }, [errorMessage]);

  // Auto-advance when MFA is required
  useEffect(() => {
    if (mfaToken) {
      // Move to TOTP step when token is available
      nextStep();
    }
  }, [mfaToken, nextStep]);
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsxs(View, {
      style: styles.modernUserProfileContainer,
      children: [/*#__PURE__*/_jsxs(View, {
        style: styles.avatarContainer,
        children: [/*#__PURE__*/_jsx(Avatar, {
          name: userProfile?.displayName || userProfile?.name || username,
          size: 100,
          theme: theme,
          style: styles.modernUserAvatar,
          backgroundColor: colors.primary + '20'
        }), /*#__PURE__*/_jsx(View, {
          style: [styles.statusIndicator, {
            backgroundColor: colors.primary
          }]
        })]
      }), /*#__PURE__*/_jsx(Text, {
        style: [styles.modernUserDisplayName, {
          color: colors.text
        }],
        children: userProfile?.displayName || userProfile?.name || username
      }), /*#__PURE__*/_jsxs(Text, {
        style: [styles.modernUsernameSubtext, {
          color: colors.secondaryText
        }],
        children: ["@", username]
      })]
    }), /*#__PURE__*/_jsxs(View, {
      style: styles.modernInputContainer,
      children: [/*#__PURE__*/_jsx(TextField, {
        ref: inputRef,
        label: t('common.labels.password'),
        leading: /*#__PURE__*/_jsx(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__*/_jsxs(View, {
        style: {
          flexDirection: 'row',
          alignItems: 'center',
          marginBottom: 16
        },
        children: [/*#__PURE__*/_jsxs(Text, {
          style: [styles.footerText, {
            color: colors.text
          }],
          children: [t('signin.forgotPrompt') || 'Forgot your password?', " "]
        }), /*#__PURE__*/_jsx(TouchableOpacity, {
          onPress: () => navigate('RecoverAccount', {
            returnTo: 'SignIn',
            returnStep: 1,
            returnData: {
              username,
              userProfile
            }
          }),
          children: /*#__PURE__*/_jsx(Text, {
            style: [styles.modernLinkText, {
              color: colors.primary
            }],
            children: t('common.links.recoverAccount')
          })
        })]
      })]
    }), /*#__PURE__*/_jsx(GroupedPillButtons, {
      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__*/_jsxs(View, {
      style: styles.securityNotice,
      children: [/*#__PURE__*/_jsx(Ionicons, {
        name: "shield-checkmark",
        size: 14,
        color: colors.secondaryText
      }), /*#__PURE__*/_jsx(Text, {
        style: [styles.securityText, {
          color: colors.secondaryText
        }],
        children: t('signin.security.dataSecure') || 'Your data is encrypted and secure'
      })]
    })]
  });
};
export default SignInPasswordStep;
//# sourceMappingURL=SignInPasswordStep.js.map