// JSX 完整 AOT 测试
// Node.js 无 JSX，自行编写极简业务用例
// 验证：无 h 函数、无胶水、纯 TS 结构化调用

function main(): number {
    // ========== 1. 基础标签：无属性 ==========
    // <div /> → 编译为结构化对象 {tag: "div", props: null, children: null}
    let vnode1: any = <div />;
    if (typeof(vnode1) !== "object" || vnode1 === null) {
        print("FAIL: <div /> should produce an object, got " + typeof(vnode1));
        return 0;
    }
    if (vnode1.tag !== "div") {
        print("FAIL: <div /> .tag should be 'div', got " + vnode1.tag);
        return 0;
    }
    print("PASS: basic self-closing tag");

    // ========== 2. 标签 + 文本子节点 ==========
    // <div>hello</div> → {tag: "div", props: null, children: ["hello"]}
    let vnode2: any = <div>hello</div>;
    if (vnode2.tag !== "div") {
        print("FAIL: <div>hello</div> .tag should be 'div'");
        return 0;
    }
    if (typeof(vnode2.children) !== "object" || vnode2.children === null) {
        print("FAIL: <div>hello</div> should have children array");
        return 0;
    }
    if (vnode2.children.length !== 1 || vnode2.children[0] !== "hello") {
        print("FAIL: <div>hello</div> children[0] should be 'hello', got " + vnode2.children[0]);
        return 0;
    }
    print("PASS: tag with text child");

    // ========== 3. 属性标签 ==========
    // <div class="container" id="main" /> → 结构化对象带 props
    let vnode3: any = <div class="container" id="main" />;
    if (vnode3.tag !== "div") {
        print("FAIL: <div class /> .tag should be 'div'");
        return 0;
    }
    if (typeof(vnode3.props) !== "object") {
        print("FAIL: <div class /> should have props object");
        return 0;
    }
    if (vnode3.props.class !== "container") {
        print("FAIL: .props.class should be 'container', got " + vnode3.props.class);
        return 0;
    }
    if (vnode3.props.id !== "main") {
        print("FAIL: .props.id should be 'main', got " + vnode3.props.id);
        return 0;
    }
    print("PASS: tag with attributes");

    // ========== 4. 嵌套子节点 ==========
    // <ul><li>A</li><li>B</li></ul>
    let vnode4: any = <ul><li>A</li><li>B</li></ul>;
    if (vnode4.tag !== "ul") {
        print("FAIL: <ul> .tag should be 'ul'");
        return 0;
    }
    if (typeof(vnode4.children) !== "object" || vnode4.children === null) {
        print("FAIL: <ul> should have children");
        return 0;
    }
    if (vnode4.children.length !== 2) {
        print("FAIL: <ul> should have 2 children, got " + vnode4.children.length);
        return 0;
    }
    let child1 = vnode4.children[0];
    let child2 = vnode4.children[1];
    if (child1.tag !== "li" || child1.children[0] !== "A") {
        print("FAIL: <ul> first child should be <li>A</li>");
        return 0;
    }
    if (child2.tag !== "li" || child2.children[0] !== "B") {
        print("FAIL: <ul> second child should be <li>B</li>");
        return 0;
    }
    print("PASS: nested children");

    // ========== 5. 属性 + 子节点混合 ==========
    // <div class="box"><span>text</span></div>
    let vnode5: any = <div class="box"><span>text</span></div>;
    if (vnode5.tag !== "div") {
        print("FAIL: mixed .tag should be 'div'");
        return 0;
    }
    if (vnode5.props.class !== "box") {
        print("FAIL: mixed .props.class should be 'box'");
        return 0;
    }
    if (vnode5.children.length !== 1 || vnode5.children[0].tag !== "span") {
        print("FAIL: mixed children[0] should be <span>");
        return 0;
    }
    print("PASS: attributes + children mixed");

    // ========== 6. 多个子节点混合 ==========
    // <div><span>A</span><span>B</span><span>C</span></div>
    let vnode6: any = <div><span>A</span><span>B</span><span>C</span></div>;
    if (vnode6.children.length !== 3) {
        print("FAIL: 3 span children should be present");
        return 0;
    }
    if (vnode6.children[1].children[0] !== "B") {
        print("FAIL: second span should contain 'B'");
        return 0;
    }
    print("PASS: multiple children");

    // ========== 7. 空子节点 ==========
    let vnode7: any = <div></div>;
    if (vnode7.tag !== "div") {
        print("FAIL: empty div tag should be 'div'");
        return 0;
    }
    print("PASS: empty children");

    // ========== 8. 文本节点含特殊字符 ==========
    let specialText = "hello & world";
    let vnode8: any = <div>{specialText}</div>;
    if (vnode8.tag !== "div") {
        print("FAIL: expression child .tag should be 'div'");
        return 0;
    }
    print("PASS: expression child");

    // ========== 验证无 h 函数 ==========
    // 如果编译成功且没有 "h is not defined" 错误，说明无胶水结构生效
    // JSX 被编译为纯结构化 {tag, props, children} 对象，不需要 h() 调用

    print("PASS: no h function needed (verified by successful compilation)");

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