import { nanoid } from "nanoid";
export function createArchitectureTool(ws, diagrams) {
    return {
        name: "architecture.generate",
        description: "Generate and display a Mermaid architecture diagram of the current project. " +
            "Analyze the project structure, dependencies, modules, and data flow, then produce " +
            "a valid Mermaid diagram (flowchart, graph, C4, etc.). The diagram will be rendered " +
            "in the editor Architecture tab and saved to .jait/architecture.mmd. Output the Mermaid source as the `diagram` parameter.",
        tier: "standard",
        category: "browser",
        source: "builtin",
        parameters: {
            type: "object",
            properties: {
                diagram: {
                    type: "string",
                    description: "The complete Mermaid diagram source code. Must be valid Mermaid syntax " +
                        "(e.g. flowchart TD, graph LR, C4Context, etc.). Do not wrap in code fences.",
                },
            },
            required: ["diagram"],
        },
        async execute(input, context) {
            const diagram = input.diagram?.trim();
            if (!diagram) {
                return { ok: false, message: "No diagram content provided" };
            }
            const projectRoot = context.projectRoot?.trim();
            if (!projectRoot) {
                return { ok: false, message: "A project is required to store architecture diagrams" };
            }
            const requestId = nanoid();
            const filePath = typeof diagrams?.getFilePath === "function"
                ? diagrams.getFilePath(projectRoot)
                : undefined;
            const saved = await diagrams?.save({
                projectRoot,
                diagram,
                userId: context.userId,
            });
            // Push the diagram to the frontend via WS
            if (ws) {
                ws.sendUICommand({
                    command: "architecture.update",
                    data: { diagram, requestId, projectRoot, filePath: saved?.filePath ?? filePath },
                }, context.sessionId);
            }
            return {
                ok: true,
                message: "Architecture diagram saved to .jait/architecture.mmd and sent to the editor.",
                data: {
                    requestId,
                    diagramLength: diagram.length,
                    projectRoot,
                    filePath: saved?.filePath ?? filePath ?? null,
                    updatedAt: saved?.updatedAt ?? null,
                },
            };
        },
    };
}
//# sourceMappingURL=architecture-tools.js.map