import { isAbsolute, resolve } from "node:path";
import { z } from "zod";
const DiffToolSchema = z
    .object({
    command: z.string().trim().min(1),
    args: z.array(z.string()).optional(),
    env: z.record(z.string(), z.string()).optional(),
})
    .passthrough();
export function parseDiffToolConfig(raw, sourceLabel) {
    if (raw === undefined) {
        return { errors: [] };
    }
    const parsed = DiffToolSchema.safeParse(raw);
    if (!parsed.success) {
        const issues = parsed.error.issues;
        if (issues.some((issue) => issue.path[0] === "command")) {
            return { errors: [`${sourceLabel}: diffTool.command must be a non-empty string.`] };
        }
        if (issues.some((issue) => issue.path[0] === "args")) {
            return { errors: [`${sourceLabel}: diffTool.args must be a string array.`] };
        }
        if (issues.some((issue) => issue.path[0] === "env")) {
            return { errors: [`${sourceLabel}: diffTool.env must be an object of string values.`] };
        }
        return { errors: [`${sourceLabel}: 'diffTool' must be an object.`] };
    }
    return {
        config: parsed.data,
        errors: [],
    };
}
export function resolveDiffToolConfig(level, config) {
    const command = resolveDiffToolCommand(level.levelRoot, config.command);
    return {
        ...config,
        command,
        ...(config.args ? { args: [...config.args] } : {}),
        ...(config.env ? { env: { ...config.env } } : {}),
    };
}
function resolveDiffToolCommand(levelRoot, command) {
    if (isAbsolute(command)) {
        return command;
    }
    if (command.startsWith("./") || command.startsWith("../") || command.includes("/")) {
        return resolve(levelRoot, command);
    }
    return command;
}
//# sourceMappingURL=diff_tool.js.map