/**
 * Phase 0 — dist 旧产物风险检查。
 */
import fs from "node:fs";
import path from "node:path";
export function check(ctx, hardFail, _info) {
    const rootDir = ctx.rootDir;
    const srcDir = path.join(rootDir, "src");
    const distDir = path.join(rootDir, "dist");
    if (!fs.existsSync(distDir)) {
        // [ENG-10] 尚未构建不是发布阻断条件，仅提示
        if (_info)
            _info("dist/ 目录不存在 — 尚未构建，跳过新鲜度检查");
        return;
    }
    let latestSrc = new Date(0);
    function findLatestSrc(dir) {
        const entries = fs.readdirSync(dir, { withFileTypes: true });
        for (const entry of entries) {
            const full = path.join(dir, entry.name);
            if (entry.isDirectory()) {
                if (entry.name === "node_modules")
                    continue;
                findLatestSrc(full);
            }
            else if (entry.isFile() && entry.name.endsWith(".ts")) {
                const stat = fs.statSync(full);
                if (stat.mtime > latestSrc)
                    latestSrc = stat.mtime;
            }
        }
    }
    findLatestSrc(srcDir);
    let oldestDist = new Date(Number.MAX_SAFE_INTEGER); // 远未来初始值，确保第一个文件一定更早
    let distFileCount = 0;
    function findOldestDist(dir) {
        const entries = fs.readdirSync(dir, { withFileTypes: true });
        for (const entry of entries) {
            const full = path.join(dir, entry.name);
            if (entry.isDirectory()) {
                findOldestDist(full);
            }
            else if (entry.isFile() && entry.name.endsWith(".js")) {
                distFileCount++;
                const stat = fs.statSync(full);
                if (stat.mtime < oldestDist)
                    oldestDist = stat.mtime;
            }
        }
    }
    findOldestDist(distDir);
    if (distFileCount === 0) {
        hardFail("DIST_STALE", "dist/ 中无 .js 文件，请先 npm run build", ["dist/"], "构建系统", "旧 gate 不检查 dist 是否为空", "构建");
    }
    else if (oldestDist < latestSrc) {
        hardFail("DIST_STALE", `dist/ 最旧文件 (${oldestDist.toISOString()}) 早于 src/ 最新文件 (${latestSrc.toISOString()})，存在过期风险`, ["dist/", "src/"], "构建系统", "旧 gate 不检查 dist 与 src 的时间一致性", "构建");
    }
}
//# sourceMappingURL=checkDistFreshness.js.map