/// WASM backend for the Aver compiler.
///
/// Transforms Aver AST → WASM binary module (.wasm).
/// Uses native WASM types: Int → i64, Float → f64, Bool → i32, heap types → i32 ptr.
///
/// Default output uses `aver/*` import ABI — host provides effect implementations.
/// `--adapter wasi` mode emits WASI imports for standalone wasmtime execution.
pub(crate) mod abi;
mod emitter;
mod expr;
mod runtime;
mod types;
pub(crate) mod value;

use crate::codegen::CodegenContext;

/// Which import ABI to emit in the WASM module.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WasmAdapter {
    /// Default: `aver/*` imports. Requires a host that provides capabilities.
    #[default]
    Aver,
    /// Compatibility: `wasi_snapshot_preview1` imports. Works with standalone wasmtime.
    Wasi,
}

/// Emit a WASM binary module from the Aver codegen context.
pub fn emit_wasm(ctx: &CodegenContext) -> Result<Vec<u8>, String> {
    emit_wasm_with_adapter(ctx, WasmAdapter::default())
}

/// Emit a WASM binary module with the specified import ABI adapter.
pub fn emit_wasm_with_adapter(
    ctx: &CodegenContext,
    adapter: WasmAdapter,
) -> Result<Vec<u8>, String> {
    emitter::build_wasm_module(ctx, adapter)
}
