// src/modules/repl/signatures.rs

use crate::parser::interpreter::Interpreter;

/// Parse a function call from line/cursor, return its name if found
fn extract_function_name(line: &str, cursor_pos: usize) -> Option<String> {
    let before = &line[..cursor_pos.min(line.len())];
    let open = before.rfind('(')?;
    let call_str = &before[..open].trim_end();
    if call_str.is_empty() { return None; }
    let ident = call_str
        .chars()
        .rev()
        .take_while(|c| c.is_alphanumeric() || *c == '_' || *c == ':')
        .collect::<String>()
        .chars()
        .rev()
        .collect::<String>();
    if ident.is_empty() { None } else { Some(ident) }
}

/// Given the current line and cursor, return the function signature if available (as a string)
pub fn get_signature_for_line(line: &str, cursor_pos: usize, interp: &Interpreter) -> Option<String> {
    let fname = extract_function_name(line, cursor_pos)?;
    interp.get_signature(&fname).cloned()
}

