@@[target("rust")]

// Frame type string → Swift type spelling.
// Frame has NO type system: type names pass through VERBATIM
// (docs/frame_language.md). The name-alias table (str→String,
// int→Int, Any→Any, …) was exterminated — it contradicted the
// passthrough contract. Write Swift's own type names.
//
// What REMAINS is structural SYNTAX, not name-aliasing: Frame's
// portable nullable/array forms map to Swift's spelling — `T | nil`
// → `T?` and `T[]` → `[T]` (recursing so the inner native type is
// preserved), and the framework's `void` token → `Void` (Swift's
// no-return spelling). Everything else passes through unchanged.

@@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 if trimmed == "void" {
                        "Void".to_string()
                    } else {
                        // pipe present but not a known null sentinel — passthrough
                        trimmed.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 if trimmed == "void" {
                    "Void".to_string()
                } else {
                    trimmed.to_string()
                };
                @@:(result)
            }
        }
}
