import z from "zod";
import { createToolFactory, ToolCategory } from "../tools2/types.js";
import { debugCache, createTypeScriptCheckFunction } from "./debug-cache.js";
export const buildWriteFileToolFactory = createToolFactory("build_writeFile", ({ clark, services, }) => {
    return {
        category: ToolCategory.FILE,
        description: `
      Overwrites the entire content of a file. Use this for creating new files or completely rewriting existing files.
      This tool automatically creates any parent directories that don't exist when writing application code files.

      Before using this tool:
      - You must use the Read tool to understand existing file contents first. This tool will error if you attempt to write without reading.
      - ALWAYS prefer editing existing files with the Edit or MultiEdit tools over completely rewriting them
      - Only use this tool for new files or when the entire file content needs to be replaced

      Usage requirements:
      - This tool completely replaces all file content - use Edit/MultiEdit tools for partial changes
      - Only use emojis if explicitly requested
      - Always use relative file paths

      For making changes to existing files, use:
      - Edit tool: Single precise replacements
      - MultiEdit tool: Multiple related changes to the same file

      DO NOT USE FOR API SOURCE CODE. Use the appropriate specialized tools instead.

      When applying standard React knowledge, favor this generic tool. When applying Superblocks patterns, consider the specialized tools.
    `,
        inputSchema: z.object({
            filePath: z.string().describe("Path to the file to write"),
            content: z.string().describe("Content to write to the file"),
        }),
        execute: async ({ filePath, content }) => {
            await services.appShell.writeFile(filePath, content);
            // Immediately invalidate TypeScript cache since we just modified files
            debugCache.invalidateTypeScript();
            // Update last modification time
            clark.updateContext({
                lastFileModificationTime: Date.now(),
            });
            // Trigger eager TypeScript check after file write (debounced to avoid excessive checks)
            const typeCheckFunction = createTypeScriptCheckFunction(services.appShell, clark.context.abortController?.signal);
            debugCache.startEagerTypeScriptCheck(typeCheckFunction);
            return {
                filePath,
                contentLength: content.length,
                message: `Successfully wrote ${content.length} characters to ${filePath}`,
            };
        },
        getSummary: (input) => {
            const lineCount = input.content.split("\n").length;
            return {
                output: `Wrote ${lineCount} line${lineCount === 1 ? "" : "s"}`,
            };
        },
    };
});
//# sourceMappingURL=build-write-file.js.map