#import "modules/std.sx";
#import "modules/math";
#import "modules/build.sx";
#import "modules/std/test.sx";
pkg :: #import "tests/fixtures/testpkg";

Point :: struct { x, y: i32; }

add :: (a: i32, b: i32) -> i32 { a + b }

Counter :: protocol {
    inc :: ();
    get :: () -> i32;
}

Summable :: protocol {
    sum :: () -> i32;
}

SimpleCounter :: struct { val: i32; }

impl Counter for SimpleCounter {
    inc :: (self: *SimpleCounter) { self.val += 1; }
    get :: (self: *SimpleCounter) -> i32 { self.val }
}

impl Summable for Point {
    sum :: (self: *Point) -> i32 { self.x + self.y }
}

// Phase 2: #inline protocol for dynamic dispatch

// Phase 2: #inline protocol for dynamic dispatch
Adder :: protocol #inline {
    add :: (n: i32);
    value :: () -> i32;
}

Accumulator :: struct {
    total: i32;
}

impl Adder for Accumulator {
    add :: (self: *Accumulator, n: i32) { self.total += n; }
    value :: (self: *Accumulator) -> i32 { self.total }
}

main :: () {

    // --- Auto type erasure (AE) ---
    print("=== Auto Type Erasure ===\n");

    // AE1: function argument — concrete passed where protocol expected (no xx)
    {
        use_counter :: (c: Counter) -> i32 { c.inc(); c.inc(); c.get() }
        sc := SimpleCounter.{ val = 10 };
        result := use_counter(sc);
        print("AE1: {}\n", result);
    }

    // AE2: variable assignment — concrete to protocol variable (no xx)
    {
        acc := Accumulator.{ total = 0 };
        a: Adder = acc;
        a.add(5);
        a.add(3);
        print("AE2: {}\n", a.value());
    }

    // AE3: struct literal passed directly (no xx)
    {
        use_counter :: (c: Counter) -> i32 { c.inc(); c.inc(); c.get() }
        result := use_counter(SimpleCounter.{ val = 100 });
        print("AE3: {}\n", result);
    }

    // AE4: explicit xx still works (not broken)
    {
        use_counter :: (c: Counter) -> i32 { c.inc(); c.get() }
        result := use_counter(xx SimpleCounter.{ val = 50 });
        print("AE4: {}\n", result);
    }

    // AE5: pointer auto-erasure — *ConcreteType to protocol
    {
        use_adder :: (a: Adder) { a.add(10); }
        acc := Accumulator.{ total = 5 };
        p := @acc;
        use_adder(p);
        print("AE5: {}\n", acc.total);
    }
}
