// === Shim 机制测试 ===
// 验证 ts-native 能正确解析 shim 包的导入并生成对应的 runtime 调用

import { clone, isEqual, range, flatten, uniq, compact, debounce, throttle } from "lodash";
import dayjs from "dayjs";
import { v4 } from "uuid";
import clsx from "clsx";
import chalk from "chalk";

// lodash shim 测试
function testLodash(): number {
    // clone — 深拷贝
    let obj = { a: 1, b: [2, 3] };
    let copied = clone(obj);

    // isEqual — 深度比较
    let equal = isEqual(obj, copied);

    // range — 生成数组
    let nums = range(1, 5);

    // flatten — 扁平化
    let nested = [1, [2, [3]]];
    let flat = flatten(nested);

    // uniq — 去重
    let dup = [1, 2, 2, 3, 3];
    let unique = uniq(dup);

    // compact — 去除假值
    let withFalsy = [0, 1, false, 2, "", 3];
    let clean = compact(withFalsy);

    // debounce / throttle — 函数节流（签名验证）
    let debounced = debounce(() => { return 1; }, 100);
    let throttled = throttle(() => { return 2; }, 200);

    console.log("lodash shim: clone, isEqual, range, flatten, uniq, compact, debounce, throttle");
    return 1;
}

// dayjs shim 测试
function testDayjs(): number {
    // dayjs() — 当前时间
    let now = dayjs();

    // format — 格式化（通过 dayjs.format 调用）
    // 注意：dayjs shim 的 format 接受 (date, template) 参数
    let formatted = dayjs.format(now, "YYYY-MM-DD");

    console.log("dayjs shim: dayjs, format");
    return 1;
}

// uuid shim 测试
function testUuid(): number {
    // v4 — 随机 UUID
    let id = v4();

    console.log("uuid shim: v4");
    return 1;
}

// clsx shim 测试
function testClsx(): number {
    // clsx — 条件类名拼接
    let classes = clsx("base", { active: true, disabled: false }, "extra");

    console.log("clsx shim: clsx");
    return 1;
}

// chalk shim 测试
function testChalk(): number {
    // chalk — 颜色输出
    let red = chalk.red("error");
    let green = chalk.green("success");
    let bold = chalk.bold("important");
    let blue = chalk.blue("info");
    let yellow = chalk.yellow("warning");
    let cyan = chalk.cyan("debug");
    let gray = chalk.gray("dim");

    console.log("chalk shim: red, green, bold, blue, yellow, cyan, gray");
    return 1;
}

// 主函数 — 运行所有测试
function main(): number {
    let passed = 0;

    passed += testLodash();
    passed += testDayjs();
    passed += testUuid();
    passed += testClsx();
    passed += testChalk();

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