// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

use crate::app::App;
use crate::event_loop::{ AppData, SurfaceFocus };
use crate::types::Point;
use crate::widget::WidgetHandlers;

impl<A: App> AppData<A>
{
	/// If the press at `pos` lands on a password-toggle icon zone of
	/// the widget at `idx`, push the toggle message and return
	/// `true` so the caller can skip the rest of the cursor /
	/// selection placement that would otherwise consume the press.
	/// Returns `false` when there is no toggle on that widget or
	/// the press fell outside the icon's hit area, leaving the
	/// caller to dispatch the press normally.
	pub( crate ) fn handle_password_toggle_press
	(
		&mut self,
		focus: SurfaceFocus,
		idx:   usize,
		pos:   Point,
	) -> bool
	{
		let toggle_msg = self.surface( focus ).frame.widget_rects.iter()
			.find( |w| w.flat_idx == idx )
			.and_then( |w| match &w.handlers
			{
				WidgetHandlers::TextEdit { password_toggle_msg: Some( msg ), .. } =>
				{
					let zone = crate::widget::text_edit::password_toggle_hit_zone( w.rect );
					if zone.contains( pos ) { Some( msg.clone() ) } else { None }
				}
				_ => None,
			} );
		if let Some( msg ) = toggle_msg
		{
			self.pending_msgs.push( msg );
			self.surface_mut( focus ).request_redraw();
			true
		}
		else
		{
			false
		}
	}
}
