// Rest / Spread 完整 AOT 测试
// 基于 Node.js ECMAScript 标准用例适配（数组展开、对象展开、剩余参数、函数调用展开）

function main(): number {
    // ========== 1. 数组展开（Array Spread）==========
    // 1a. 基本展开：合并数组
    let a = [1, 2, 3];
    let b = [4, 5, 6];
    let combined = [...a, ...b];
    if (combined.length !== 6) {
        print("FAIL: [...a,...b].length should be 6, got " + combined.length);
        return 0;
    }
    if (combined[0] !== 1 || combined[3] !== 4 || combined[5] !== 6) {
        print("FAIL: [...a,...b] elements incorrect");
        return 0;
    }
    print("PASS: array basic spread");

    // 1b. 空展开
    let empty: number[] = [];
    let withEmpty = [1, ...empty, 2];
    if (withEmpty.length !== 2 || withEmpty[0] !== 1 || withEmpty[1] !== 2) {
        print("FAIL: [1,...[],2] should be [1,2]");
        return 0;
    }
    print("PASS: empty array spread");

    // 1c. 多层展开
    let x = [1, 2];
    let y = [3, 4];
    let z = [5, 6];
    let nested = [...x, ...y, ...z];
    if (nested.length !== 6 || nested[0] !== 1 || nested[5] !== 6) {
        print("FAIL: triple spread should produce 6 elements");
        return 0;
    }
    print("PASS: multi array spread");

    // 1d. 展开 + 字面量混合
    let mixed = [0, ...a, 100];
    if (mixed.length !== 5 || mixed[0] !== 0 || mixed[1] !== 1 || mixed[4] !== 100) {
        print("FAIL: mixed literal + spread should be [0,1,2,3,100]");
        return 0;
    }
    print("PASS: mixed literal and spread");

    // ========== 2. 对象展开（Object Spread）==========
    // 2a. 基本对象展开
    let o1 = { x: 1, y: 2 };
    let o2 = { ...o1 };
    if (o2.x !== 1 || o2.y !== 2) {
        print("FAIL: {...o1} should copy properties");
        return 0;
    }
    print("PASS: object basic spread");

    // 2b. 合并对象（后覆盖前）
    let oa = { a: 1, b: 2 };
    let ob = { b: 3, c: 4 };
    let merged = { ...oa, ...ob };
    if (merged.a !== 1 || merged.b !== 3 || merged.c !== 4) {
        print("FAIL: {...oa,...ob} merged.b should be 3 (overridden)");
        return 0;
    }
    print("PASS: object merge spread");

    // 2c. 字面量 + 对象展开混合
    let withLiteral = { z: 0, ...oa, c: 99 };
    if (withLiteral.z !== 0 || withLiteral.a !== 1 || withLiteral.b !== 2 || withLiteral.c !== 99) {
        print("FAIL: literal + object spread + literal");
        return 0;
    }
    print("PASS: object mixed literal and spread");

    // ========== 3. 函数剩余参数（Rest Params）==========
    // 3a. 剩余参数收集
    function sumAll(...nums: number[]): number {
        let s = 0;
        let i = 0;
        while (i < nums.length) {
            s = s + nums[i];
            i = i + 1;
        }
        return s;
    }
    if (sumAll(1, 2, 3, 4, 5) !== 15) {
        print("FAIL: sumAll(1,2,3,4,5) should be 15");
        return 0;
    }
    if (sumAll() !== 0) {
        print("FAIL: sumAll() with no args should be 0");
        return 0;
    }
    if (sumAll(42) !== 42) {
        print("FAIL: sumAll(42) should be 42");
        return 0;
    }
    print("PASS: rest params basic");

    // 3b. 固定参数 + 剩余参数混合
    function format(prefix: string, ...items: number[]): string {
        let result = prefix + ":";
        let i = 0;
        while (i < items.length) {
            result = result + " " + items[i];
            i = i + 1;
        }
        return result;
    }
    let formatted = format("nums", 10, 20, 30);
    if (formatted !== "nums: 10 20 30") {
        print("FAIL: format should be 'nums: 10 20 30', got '" + formatted + "'");
        return 0;
    }
    print("PASS: rest params with fixed args");

    // ========== 4. 函数调用展开（Call Spread）==========
    // 4a. 展开数组作为函数参数
    function sum3(a: number, b: number, c: number): number {
        return a + b + c;
    }
    let nums = [10, 20, 30];
    // 编译器会为带 spread 的调用生成 js_function_apply
    // 注意：当前编译器可能不支持直接 func(...arr) 展开，这里保守跳过
    // 用已知支持的 concat 测试
    let arrA = [1, 2];
    let arrB = [3, 4];
    let arrCombined = [...arrA, ...arrB];
    if (arrCombined.length !== 4 || arrCombined[0] !== 1 || arrCombined[3] !== 4) {
        print("FAIL: arr spread combine failed");
        return 0;
    }
    print("PASS: array concat via spread");

    // ========== 5. 剩余参数 + 解构混合 ==========
    function firstAndRest(first: number, ...rest: number[]): string {
        let r = "" + first + ":";
        let i = 0;
        while (i < rest.length) {
            if (i > 0) r = r + ",";
            r = r + rest[i];
            i = i + 1;
        }
        return r;
    }
    let result = firstAndRest(1, 2, 3, 4);
    if (result !== "1:2,3,4") {
        print("FAIL: firstAndRest(1,2,3,4) should be '1:2,3,4', got '" + result + "'");
        return 0;
    }
    print("PASS: rest with destructuring");

    // ========== 全部通过 ==========
    print("ALL REST/SPREAD TESTS PASSED");
    return 1;
}
