export function initializeInputToggle($container) {
    const $input = $container.find('input');
    const $toggle = $container.find('.toggle-show');

    if (!$input.length || !$toggle.length) return;

    const handleToggleClick = (e) => {
        e.preventDefault();

        const isPassword = String($input.attr('type')) === 'password';
        $input.attr('type', isPassword ? 'text' : 'password');
        const $icon = $toggle.find('svg, i').first();

        if ($icon.length) {
            $icon
                .removeClass('fa-eye fa-eye-slash')
                .addClass(isPassword ? 'fa-eye' : 'fa-eye-slash');
        }
    };

    const updateToggleVisibility = () => {
        $toggle.toggle(($input.val() || '').length > 0);
    };

    $toggle
        .off('click.passwordToggle mousedown.passwordToggle')
        .on('mousedown.passwordToggle', handleToggleClick);

    $input.off('input.passwordToggle').on('input.passwordToggle', updateToggleVisibility);
    updateToggleVisibility();
}
