import { z } from "zod";
import { getPrefixedLogger } from "../../../../util/logger.js";
import { createToolFactory, ToolCategory } from "../../tools2/types.js";
export const writeApiToolFactory = createToolFactory("writeApi", (services) => {
    return {
        category: ToolCategory.API,
        description: `
Stage API source code for building. This prepares the API source but does not compile or save it yet.

After calling this tool, you must call buildApi to compile, validate, and save the API.

IMPORTANT you must read the relevant API documentation before using this tool

Use this tool to:
- Create new API source code
- Update existing API source code (full replacement)
`,
        inputSchema: z.object({
            apiName: z.string().describe("The name of the API"),
            apiSource: z
                .string()
                .describe("The complete TypeScript source code of the API"),
            action: z
                .enum(["create", "edit"])
                .describe("Whether this is creating a new API or editing an existing one"),
        }),
        execute: async ({ apiName, apiSource, action }) => {
            const logger = getPrefixedLogger(`[writeApi-${apiName}]`);
            const apiTsFilePath = `apis/${apiName}.ts`;
            await services.appShell.writeFile(apiTsFilePath, apiSource);
            logger.info(`API source staged for ${apiName} (action: ${action}, length: ${apiSource.length} chars)`);
            return {
                message: `API source for ${apiName} written to ${apiTsFilePath}. Call buildApi to compile, validate, and save the API.`,
                apiName,
                action,
                sourceLength: apiSource.length,
            };
        },
        getSummary: (input) => {
            const lineCount = input.apiSource.split("\n").length;
            return {
                output: `Wrote ${lineCount} line${lineCount === 1 ? "" : "s"}`,
            };
        },
    };
});
//# sourceMappingURL=write-api.js.map