// === QuickJS 嵌入测试 ===
// 验证 ts-native 能正确配置 QuickJS fallback 并生成 js_qjs_call 调用

import { get, post } from "axios";
import express from "express";
import { flow, curry } from "lodash-fp";
import moment from "moment";
import { compile } from "handlebars";

// axios fallback 测试
function testAxios(): number {
    // GET 请求 — 通过 QuickJS 执行 axios.get
    let data = get("https://api.example.com/users");

    // POST 请求 — 通过 QuickJS 执行 axios.post
    let result = post("https://api.example.com/data", { name: "test" });

    console.log("axios QuickJS fallback: get, post");
    return 1;
}

// express fallback 测试
function testExpress(): number {
    // 创建 express 应用 — 通过 QuickJS 执行
    let app = express();

    // 路由定义 — 通过 QuickJS 执行
    app.get("/api", () => { return { status: "ok" }; });
    app.post("/api/data", () => { return { created: true }; });

    console.log("express QuickJS fallback: app, get, post");
    return 1;
}

// lodash-fp fallback 测试
function testLodashFp(): number {
    // 函数式编程 — 通过 QuickJS 执行
    let transform = flow([curry((x: number) => x * 2)]);
    let result = transform(5);

    console.log("lodash-fp QuickJS fallback: flow, curry");
    return 1;
}

// moment fallback 测试
function testMoment(): number {
    // 日期处理 — 通过 QuickJS 执行
    let now = moment();
    let formatted = moment.format(now, "YYYY-MM-DD HH:mm:ss");

    console.log("moment QuickJS fallback: moment, format");
    return 1;
}

// handlebars fallback 测试
function testHandlebars(): number {
    // 模板编译 — 通过 QuickJS 执行
    let tpl = compile("Hello, {{name}}!");
    let html = tpl({ name: "ts-native" });

    console.log("handlebars QuickJS fallback: compile");
    return 1;
}

function main(): number {
    let passed = 0;

    passed += testAxios();
    passed += testExpress();
    passed += testLodashFp();
    passed += testMoment();
    passed += testHandlebars();

    console.log("QuickJS fallback test: " + passed + "/5 groups passed");
    return passed;
}
