"use strict";

import { useRef, useEffect } from 'react';
import { View, Text } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import HighFive from '../../../assets/illustrations/HighFive';
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 SignInUsernameStep = ({
  colors,
  styles,
  navigate,
  nextStep,
  username,
  setUsername,
  errorMessage,
  setErrorMessage,
  validationStatus,
  userProfile,
  isValidating,
  isAddAccountMode,
  user,
  validateUsername
}) => {
  const inputRef = useRef(null);
  const {
    t
  } = useI18n();

  // Monitor username prop changes
  useEffect(() => {
    console.log('👀 SignInUsernameStep username prop changed:', username);
  }, [username]);
  const handleUsernameChange = text => {
    console.log('📝 Username input changed:', text);
    setUsername(text);
    if (errorMessage) setErrorMessage('');
  };
  const handleContinue = async () => {
    console.log('🚀 Continue button pressed, username:', username);
    const trimmedUsername = username?.trim() || '';
    if (!trimmedUsername) {
      console.log('❌ Username is empty');
      setErrorMessage('Please enter your username.');
      setTimeout(() => inputRef.current?.focus(), 0);
      return;
    }
    if (trimmedUsername.length < 2) {
      console.log('❌ Username too short');
      setErrorMessage('Username must be at least 3 characters.');
      return;
    }
    console.log('🔍 Starting username validation...');
    try {
      // Validate the username before proceeding
      const isValid = await validateUsername(trimmedUsername);
      console.log('📊 Validation result:', isValid);
      if (isValid) {
        console.log('✅ Validation passed, proceeding to next step');
        nextStep();
      } else {
        console.log('❌ Validation failed, staying on current step');
      }
    } catch (error) {
      console.error('🚨 Error during validation:', error);
      setErrorMessage('Unable to validate username. Please try again.');
    }
  };
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsx(HighFive, {
      width: 100,
      height: 100
    }), /*#__PURE__*/_jsxs(View, {
      style: styles.modernHeader,
      children: [/*#__PURE__*/_jsx(Text, {
        style: [styles.modernTitle, {
          color: colors.text
        }],
        children: isAddAccountMode ? t('signin.addAccountTitle') : t('signin.title')
      }), /*#__PURE__*/_jsx(Text, {
        style: [styles.modernSubtitle, {
          color: colors.secondaryText
        }],
        children: isAddAccountMode ? t('signin.addAccountSubtitle') : t('signin.subtitle')
      })]
    }), isAddAccountMode && user && /*#__PURE__*/_jsxs(View, {
      style: [styles.modernInfoCard, {
        backgroundColor: colors.inputBackground
      }],
      children: [/*#__PURE__*/_jsx(Ionicons, {
        name: "information-circle",
        size: 20,
        color: colors.primary
      }), /*#__PURE__*/_jsxs(Text, {
        style: [styles.modernInfoText, {
          color: colors.text
        }],
        children: [t('signin.currentlySignedInAs', {
          username: user.username
        }) || 'Currently signed in as ', /*#__PURE__*/_jsx(Text, {
          style: {
            fontWeight: 'bold'
          },
          children: user.username
        })]
      })]
    }), /*#__PURE__*/_jsx(View, {
      style: styles.modernInputContainer,
      children: /*#__PURE__*/_jsx(TextField, {
        ref: inputRef,
        label: t('common.labels.username'),
        leading: /*#__PURE__*/_jsx(Ionicons, {
          name: "person-outline",
          size: 24,
          color: colors.secondaryText
        }),
        value: username,
        onChangeText: handleUsernameChange,
        autoCapitalize: "none",
        autoCorrect: false,
        testID: "username-input",
        variant: "filled",
        error: validationStatus === 'invalid' ? errorMessage : undefined,
        loading: validationStatus === 'validating',
        success: validationStatus === 'valid',
        onSubmitEditing: () => handleContinue(),
        autoFocus: true
      })
    }), /*#__PURE__*/_jsx(GroupedPillButtons, {
      buttons: [{
        text: t('common.links.signUp'),
        onPress: () => navigate('SignUp'),
        icon: 'person-add',
        variant: 'transparent'
      }, {
        text: t('common.actions.continue'),
        onPress: handleContinue,
        icon: 'arrow-forward',
        variant: 'primary',
        loading: isValidating,
        disabled: !username || username.trim().length < 2 || isValidating,
        testID: 'username-next-button'
      }],
      colors: colors
    })]
  });
};
export default SignInUsernameStep;
//# sourceMappingURL=SignInUsernameStep.js.map