import { isRecord, stringField } from "@best-agent/foundation";
import { isAbsolute, resolve } from "node:path";
import { isToolInputRecord } from "../../tool/tool-history-input.js";
export function extractWorkspaceMutationCandidatePaths(input, cwd) {
    const paths = [];
    collectWorkspaceMutationCandidatePaths(input, paths);
    return Array.from(new Set(paths.map((path) => (isAbsolute(path) ? path : resolve(cwd, path)))));
}
export function workspaceSnapshotPathsKey(paths, cwd) {
    const resolvedPaths = Array.from(new Set(paths.map((path) => (isAbsolute(path) ? path : resolve(cwd, path))))).sort();
    return resolvedPaths.length > 0 ? resolvedPaths.join("\0") : undefined;
}
export function readWorkspaceSnapshotDigest(payload) {
    const snapshot = payload.workspaceSnapshot;
    if (!isRecord(snapshot)) {
        return undefined;
    }
    const digest = stringField(snapshot, "digest");
    const pathsKey = stringField(snapshot, "pathsKey");
    const paths = snapshot.paths;
    if (digest === undefined ||
        pathsKey === undefined ||
        !Array.isArray(paths) ||
        !paths.every((path) => typeof path === "string")) {
        return undefined;
    }
    return { digest, paths, pathsKey };
}
function collectWorkspaceMutationCandidatePaths(input, paths) {
    if (typeof input === "string") {
        return;
    }
    if (Array.isArray(input)) {
        for (const entry of input) {
            collectWorkspaceMutationCandidatePaths(entry, paths);
        }
        return;
    }
    if (!isToolInputRecord(input)) {
        return;
    }
    const value = input.file_path;
    if (typeof value === "string" && value.trim()) {
        paths.push(value);
    }
}
//# sourceMappingURL=workspace-mutation-paths.js.map