use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::text::{Line, Text};
use ratatui::widgets::{Block, Borders, Paragraph};
use crate::shared::i18n::Locale;
use crate::shared::theme::Palette;
use crate::shared::wrap::wrap_ranges;
pub fn render(
frame: &mut Frame,
area: Rect,
text: &str,
tick: usize,
done: bool,
palette: &Palette,
loc: &'static Locale,
) {
let hint = if done {
loc.t("ui.impersonation.done").to_string()
} else {
let frames = palette.glyphs().spinner;
let spinner = frames[(tick / 2) % frames.len()];
loc.tf(
"ui.impersonation.active",
&[("spinner", &spinner.to_string())],
)
};
let block = Block::default()
.borders(Borders::ALL)
.border_type(palette.glyphs().border)
.border_style(palette.accent_style())
.title(Line::from(hint).style(palette.accent_style()));
let inner_w = area.width.saturating_sub(2) as usize;
let inner_h = area.height.saturating_sub(2) as usize;
let mut rows: Vec<Line<'static>> = Vec::new();
for logical in text.split('\n') {
let chars: Vec<char> = logical.chars().collect();
for (s, e) in wrap_ranges(&chars, inner_w) {
rows.push(Line::from(chars[s..e].iter().collect::<String>()));
}
}
let scroll = (rows.len().saturating_sub(inner_h)) as u16;
let para = Paragraph::new(Text::from(rows))
.block(block)
.scroll((scroll, 0));
frame.render_widget(para, area);
}