@@[target("rust")]

// Frame type string → Swift type spelling. This is the most
// interesting Frame fit in Round 2: the mapper recurses (for
// `T | nil` and `T[]` suffixes), and Frame's `@@:(value)` only
// SETS the return value — it does not return early. So the body
// has to compute one final answer through a chain of
// if-let / else and call `@@:(...)` exactly once at the end.
// Worth recording as a Frame ergonomics observation in
// RFC-0035: the natural Rust idiom (early-return on prefix
// match) doesn't translate verbatim. We work around it cleanly
// here, but the native Rust function is shorter and clearer.

@@system SwiftMapType {
    interface:
        map(t: String): String

    machine:
        $Active {
            map(t: String): String {
                let trimmed = t.trim();
                let result = if let Some(pipe_pos) = trimmed.find('|') {
                    let base = trimmed[..pipe_pos].trim();
                    let suffix = trimmed[pipe_pos + 1..].trim();
                    if suffix == "nil" || suffix == "null" || suffix == "None" {
                        let inner = crate::frame_c::compiler::type_map::swift_map_type(base);
                        format!("{}?", inner)
                    } else {
                        // pipe present but not a known null sentinel — fall through
                        match trimmed {
                            "Any" | "Object" | "object" => "Any".to_string(),
                            "str" | "string" | "String" => "String".to_string(),
                            "int" | "i32" | "i64" | "number" => "Int".to_string(),
                            "float" | "f64" | "f32" | "double" => "Double".to_string(),
                            "bool" | "boolean" | "Boolean" => "Bool".to_string(),
                            "void" => "Void".to_string(),
                            other => other.to_string(),
                        }
                    }
                } else if let Some(base) = trimmed.strip_suffix("[]") {
                    let inner = crate::frame_c::compiler::type_map::swift_map_type(base);
                    format!("[{}]", inner)
                } else {
                    match trimmed {
                        "Any" | "Object" | "object" => "Any".to_string(),
                        "str" | "string" | "String" => "String".to_string(),
                        "int" | "i32" | "i64" | "number" => "Int".to_string(),
                        "float" | "f64" | "f32" | "double" => "Double".to_string(),
                        "bool" | "boolean" | "Boolean" => "Bool".to_string(),
                        "void" => "Void".to_string(),
                        other => other.to_string(),
                    }
                };
                @@:(result)
            }
        }
}
