import { mkdir, writeFile } from "node:fs/promises";
import path from "node:path";
import { resolveWorkspacePath } from "../safety/guardrails.js";
import { expectNonEmptyString } from "./helpers.js";
function expectString(input, key) {
    const value = input[key];
    if (typeof value !== "string") {
        throw new Error(`Input "${key}" must be a string.`);
    }
    return value;
}
export function createWriteFileTool(options, hooks) {
    return {
        name: "write_file",
        description: "Create or overwrite a file with the provided content.",
        inputSchema: {
            type: "object",
            properties: {
                path: {
                    type: "string",
                    description: "Path to the file relative to workspace root.",
                },
                content: {
                    type: "string",
                    description: "The full file content to write.",
                },
            },
            required: ["path", "content"],
            additionalProperties: false,
        },
        execute: async (input) => {
            const requestedPath = expectNonEmptyString(input, "path");
            const content = expectString(input, "content");
            const filePath = resolveWorkspacePath(requestedPath, options.workspaceRoot);
            await mkdir(path.dirname(filePath), { recursive: true });
            await writeFile(filePath, content, "utf8");
            if (hooks?.afterWrite) {
                await hooks.afterWrite(filePath, content);
            }
            return `Wrote ${content.length} characters to "${requestedPath}".`;
        },
    };
}
//# sourceMappingURL=write-file.js.map