use night_fury_core::Selector;

pub fn parse_selector(input: &str) -> Result<Selector, String> {
    if input.is_empty() {
        return Err("click @e<N>  or  click css <selector>  or  click text <text>".into());
    }
    if let Some(rest) = input.strip_prefix("@e") {
        let idx: usize = rest.parse().map_err(|_| format!("invalid @ref: {input}"))?;
        return Ok(Selector::Ref(idx));
    }
    // "css <selector>"
    if let Some(rest) = input
        .strip_prefix("css ")
        .or_else(|| input.strip_prefix("CSS "))
    {
        return Ok(Selector::Css(rest.to_string()));
    }
    // "text <text>"
    if let Some(rest) = input.strip_prefix("text ") {
        return Ok(Selector::Text(rest.to_string()));
    }
    // bare string without prefix — treat as CSS
    Ok(Selector::Css(input.to_string()))
}
