// ESM Export 完整 AOT 测试 — 验证静态编译下所有导出/导入语义
// 基于 Node.js ECMAScript 标准用例适配

import { PI, E, add, multiply } from "./named_exports";
import greet from "./default_export";
import { doubleSquare, createUser } from "./re_exporter";
import { Item, createItem, getItemName, getItemValue } from "./aliases";
import * as All from "./star_exports";
import { STATUS, getStatus } from "./star_exports";
import { VERSION } from "./star_exports";
import * as Consts from "./const_values";

function main(): number {
    // === 1. 命名导出测试 ===
    // export const PI = 3.14159
    if (Math.abs(PI - 3.14159) > 0.00001) {
        print("FAIL: PI should be 3.14159, got " + PI);
        return 0;
    }
    // export const E = 2.71828
    if (Math.abs(E - 2.71828) > 0.00001) {
        print("FAIL: E should be 2.71828, got " + E);
        return 0;
    }
    // export function add(a, b) { return a + b; }
    if (add(10, 20) !== 30) {
        print("FAIL: add(10,20) should be 30, got " + add(10, 20));
        return 0;
    }
    // export function multiply(a, b) { return a * b; }
    if (multiply(6, 7) !== 42) {
        print("FAIL: multiply(6,7) should be 42, got " + multiply(6, 7));
        return 0;
    }
    print("PASS: named exports");

    // === 2. 默认导出测试 ===
    // export default function(name) { return "Hello " + name; }
    if (greet("World") !== "Hello World") {
        print("FAIL: greet(\"World\") should be \"Hello World\", got " + greet("World"));
        return 0;
    }
    if (greet("ESM") !== "Hello ESM") {
        print("FAIL: greet(\"ESM\") should be \"Hello ESM\", got " + greet("ESM"));
        return 0;
    }
    print("PASS: default export");

    // === 3. 重导出测试（re-export）===
    // Re-exported from named_exports: add, multiply (through re_exporter)
    if (doubleSquare(4) !== 32) {
        print("FAIL: doubleSquare(4) should be 32, got " + doubleSquare(4));
        return 0;
    }
    let user = createUser("Alice", 30);
    if (user.name !== "Alice" || user.age !== 30) {
        print("FAIL: createUser should return {name:'Alice',age:30}");
        return 0;
    }
    print("PASS: re-export");

    // === 4. 别名导出测试 ===
    // export { Item as ExportedItem } from "./named_exports"
    // Actually let's use the direct test:
    if (typeof(getItemName) !== "function") {
        print("FAIL: getItemName should be a function");
        return 0;
    }
    let item = createItem("test-item", 99);
    if (item.name !== "test-item" || item.value !== 99) {
        print("FAIL: createItem should return {name:'test-item',value:99}");
        print("  got name=" + item.name + " value=" + item.value);
        return 0;
    }
    print("PASS: alias exports");

    // === 5. 星号导出测试（export * from）===
    // export * from "./named_exports" — PI, E, add, multiply should be available
    if (Math.abs(All.PI - 3.14159) > 0.00001) {
        print("FAIL: All.PI should be 3.14159");
        return 0;
    }
    if (All.add(5, 3) !== 8) {
        print("FAIL: All.add(5,3) should be 8");
        return 0;
    }
    // Named import from star_exports (specific re-exports)
    if (STATUS !== "active") {
        print("FAIL: STATUS should be 'active', got " + STATUS);
        return 0;
    }
    if (VERSION !== "2.0.0") {
        print("FAIL: VERSION should be '2.0.0', got " + VERSION);
        return 0;
    }
    if (getStatus() !== "active") {
        print("FAIL: getStatus() should be 'active', got " + getStatus());
        return 0;
    }
    print("PASS: star exports and named imports");

    // === 6. 命名空间导入测试 ===
    if (Consts.MAX_SIZE !== 1024) {
        print("FAIL: Consts.MAX_SIZE should be 1024");
        return 0;
    }
    if (Consts.MIN_SIZE !== 0) {
        print("FAIL: Consts.MIN_SIZE should be 0");
        return 0;
    }
    if (Consts.getRange() !== 1024) {
        print("FAIL: Consts.getRange() should be 1024");
        return 0;
    }
    print("PASS: namespace import");

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