/**
 * @deprecated [DC-2] 无运行时消费方。保留供未来跨平台兼容实现。
 *
 * 问题四十: Platform Compatibility — 跨平台路径安全、规范化与兼容性。
 *
 * 强制执行:
 * - symlink / case sensitivity / path separator / shell 差异不得绕过 scope
 * - 跨平台兼容不得以降低安全边界为代价
 * - 路径判定必须使用 path.resolve + path.relative，禁止 startsWith 字符串边界判断
 */
import path from "node:path";
import fs from "node:fs";
/** 默认解析器 — 使用 Node.js 原生 path + fs */
const DEFAULT_RESOLVER = {
    resolve: (...segments) => path.resolve(...segments),
    realpath: (p) => {
        try {
            return fs.realpathSync.native(p);
        }
        catch {
            return p;
        }
    },
};
/**
 * 检测当前平台上下文。
 */
export function detectPlatformContext(workspacePath) {
    const platform = process.platform;
    return {
        os: platform === "win32" ? "win32" : platform === "darwin" ? "darwin" : "linux",
        arch: process.arch,
        shell: detectShell(),
        path_case_sensitive: platform !== "win32" && platform !== "darwin",
        path_separator: platform === "win32" ? "\\" : "/",
        line_ending: platform === "win32" ? "crlf" : "lf",
        supports_symlink: platform !== "win32",
        workspace_realpath: workspacePath,
    };
}
function detectShell() {
    const shell = process.env.SHELL ?? "";
    if (shell.includes("zsh"))
        return "zsh";
    if (shell.includes("bash"))
        return "bash";
    if (shell.includes("fish"))
        return "fish";
    if (process.platform === "win32") {
        return process.env.COMSPEC?.includes("cmd") ? "cmd" : "powershell";
    }
    return "unknown";
}
/**
 * 规范化路径引用。
 *
 * 安全策略:
 * - 使用 path.resolve 解析 .. / ~ / 相对路径
 * - 使用 path.relative + startsWith('..') 判断是否逃逸 scope
 * - 禁止 startsWith 字符串边界判断作为 scope 判定方式
 * - realpath 解析 symlink，文件不存在时回退到 resolved 路径
 */
export function normalizePathRef(rawPath, platform, scopeBoundary, resolver = DEFAULT_RESOLVER) {
    // 跨平台分隔符归一化: Windows 反斜杠统一为正斜杠后再 resolve
    const normalizedRaw = platform.os === "win32" ? rawPath.replace(/\\/g, "/") : rawPath;
    const normalizedScope = platform.os === "win32" ? scopeBoundary.replace(/\\/g, "/") : scopeBoundary;
    // path.resolve 解析所有 .. / . / 相对路径段
    const resolved = resolver.resolve(normalizedRaw);
    const resolvedScope = resolver.resolve(normalizedScope);
    // realpath 解析 symlink — 文件不存在时回退到 resolved
    const realPath = resolver.realpath(resolved);
    const realScope = resolver.realpath(resolvedScope);
    // 使用 path.relative 判断包含关系:
    // relative 结果不以 .. 开头且不是绝对路径 → 在 scope 内
    const relative = path.relative(realScope, realPath);
    const isWithinWorkspace = relative !== "" && !relative.startsWith("..") && !path.isAbsolute(relative);
    let pathSafetyStatus = "safe";
    // symlink / 逃逸检测
    const hasSymlinkIndicator = rawPath.includes("..") || rawPath.includes("~");
    // 大小写别名冲突: 仅当 resolved 与 realpath 大小写不一致时才标记
    // 普通保留大小写路径（如 /tmp/Project/src/App.ts）在 macOS 上是安全的，
    // 仅当请求路径与磁盘真实路径存在大小写别名歧义时才为冲突
    const caseNormalized = !platform.path_case_sensitive
        && resolved.toLowerCase() === realPath.toLowerCase()
        && resolved !== realPath;
    if (!isWithinWorkspace && hasSymlinkIndicator) {
        pathSafetyStatus = "symlink_escape";
    }
    else if (!isWithinWorkspace) {
        pathSafetyStatus = "outside_scope";
    }
    else if (caseNormalized) {
        pathSafetyStatus = "case_conflict";
    }
    return {
        original: rawPath,
        normalized: resolved,
        is_within_workspace: isWithinWorkspace,
        case_normalized: caseNormalized,
        symlink_resolved: realPath !== resolved,
        platform: platform.os,
        path_safety_status: pathSafetyStatus,
    };
}
/**
 * 执行路径安全检查。
 */
export function checkPathSafety(pathRef, scopeBoundary) {
    const passes = pathRef.is_within_workspace && pathRef.path_safety_status === "safe";
    let reason = "";
    if (pathRef.path_safety_status === "symlink_escape") {
        reason = "路径包含 .. 或 ~，可能逃逸工作区范围";
    }
    else if (pathRef.path_safety_status === "outside_scope") {
        reason = "路径在工作区范围外";
    }
    else if (pathRef.path_safety_status === "case_conflict") {
        reason = "不区分大小写的平台存在大小写冲突风险";
    }
    else if (passes) {
        reason = "路径安全";
    }
    return {
        path_ref: pathRef,
        scope_boundary: scopeBoundary,
        passes,
        reason,
    };
}
/**
 * 生成平台兼容性报告。
 */
export function generatePlatformReport(platform, pathsToCheck, scopeBoundary) {
    const issues = [];
    let status = "compatible";
    for (const rawPath of pathsToCheck) {
        const ref = normalizePathRef(rawPath, platform, scopeBoundary);
        if (ref.path_safety_status !== "safe") {
            issues.push(`路径不安全: ${rawPath} (${ref.path_safety_status})`);
        }
    }
    if (issues.some(i => i.includes("symlink_escape"))) {
        status = "blocked";
    }
    else if (issues.length > 0) {
        status = "needs_normalization";
    }
    return {
        platform_context: platform,
        path_refs_checked: pathsToCheck.length,
        issues_found: issues,
        platform_compatibility_status: status,
    };
}
/**
 * 创建测试用的平台上下文。
 */
export function createTestPlatformContext(overrides) {
    return {
        os: "darwin",
        arch: "arm64",
        shell: "zsh",
        path_case_sensitive: false,
        path_separator: "/",
        line_ending: "lf",
        supports_symlink: true,
        workspace_realpath: "/tmp/test-workspace",
        ...overrides,
    };
}
//# sourceMappingURL=platform_context.js.map