/**
 * Example usage of the Tool Registry system.
 *
 * This file demonstrates:
 * - Creating tool factories
 * - Registering tools
 * - Setting up access control
 * - Executing tools with permissions
 */
import { z } from "zod";
import { ToolRegistry, PermissionLevel, promptForTools, requireRole, createToolFactory, ToolCategory, } from "./index.js";
// ============================================================================
// Example Tool: Add Event
// ============================================================================
const addEventToolFactory = createToolFactory("add-event", (_deps) => {
    // This would typically import from your existing tool
    const PayloadSchema = z
        .record(z.string(), z.enum(["string", "number", "boolean"]))
        .optional();
    return {
        description: "Add a new event to the scope",
        category: ToolCategory.COMPONENT,
        defaultPermission: PermissionLevel.ALLOW,
        inputSchema: z.object({
            pageScopeId: z.string(),
            entityName: z.string(),
            payload: PayloadSchema,
        }),
        execute: async (input) => {
            // Implementation here
            console.log("Adding event:", input);
            return {
                success: true,
                message: `Event ${input.entityName} added`,
            };
        },
    };
});
// ============================================================================
// Example Tool: Delete File (Destructive)
// ============================================================================
const deleteFileToolFactory = createToolFactory("delete-file", (_fileSystem) => ({
    description: "Delete a file from the project",
    category: ToolCategory.FILE,
    defaultPermission: PermissionLevel.PROMPT, // Requires confirmation
    inputSchema: z.object({
        path: z.string(),
    }),
    execute: async (input) => {
        // Implementation here
        console.log("Deleting file:", input.path);
        return {
            success: true,
            message: `File ${input.path} deleted`,
        };
    },
}));
// ============================================================================
// Example Tool: Read File (Safe)
// ============================================================================
const readFileToolFactory = createToolFactory("read-file", (_fileSystem) => ({
    description: "Read a file from the project",
    category: ToolCategory.FILE,
    defaultPermission: PermissionLevel.ALLOW,
    inputSchema: z.object({
        path: z.string(),
    }),
    execute: async (input) => {
        console.log("Reading file:", input.path);
        return {
            content: "file contents here",
        };
    },
}));
// ============================================================================
// Setup Example
// ============================================================================
export function setupExampleRegistry() {
    const registry = new ToolRegistry();
    // 1. Setup access control rules
    //    Rules are evaluated in order, most restrictive wins
    // Block tools for guest users
    registry.addAccessRule(requireRole(["admin", "editor"], PermissionLevel.BLOCK));
    // Prompt for destructive operations
    registry.addAccessRule(promptForTools(/^delete-/));
    // Or use a preset
    // registry.addAccessRule(...presets.production());
    // 2. Setup prompt handler
    registry.setPromptHandler(async (tool, input, _permissions, context) => {
        console.log(`\n=== User Confirmation Required ===`);
        console.log(`Tool: ${tool.name}`);
        // Use tool formatting methods if available
        const actionName = tool.getActionName?.();
        const formattedInput = tool.formatInput?.(input);
        if (actionName) {
            console.log(`Action: ${actionName.future}`);
        }
        if (formattedInput) {
            if (formattedInput.targets.length > 0) {
                console.log(`Targets: ${formattedInput.targets.join(", ")}`);
            }
            if (formattedInput.summary) {
                console.log(`Summary: ${formattedInput.summary}`);
            }
            if (formattedInput.details) {
                console.log(`Details:\n${formattedInput.details}`);
            }
        }
        else {
            console.log(`Input:`, input);
        }
        console.log(`User: ${context.userId} (${context.userRole})`);
        console.log(`===================================\n`);
        return; // Mock: auto-approve for demo
    });
    // 3. Register and instantiate tools with dependencies
    const mockDeps = {};
    registry.register(addEventToolFactory, mockDeps);
    registry.register(deleteFileToolFactory, mockDeps);
    registry.register(readFileToolFactory, mockDeps);
    return registry;
}
// ============================================================================
// Usage Example
// ============================================================================
export async function exampleUsage() {
    const registry = setupExampleRegistry();
    // Example 1: Execute allowed tool
    console.log("\n--- Example 1: Allowed tool ---");
    const result1 = await registry.execute("read-file", { path: "/src/app.ts" }, { userId: "user-123", userRole: "admin" });
    console.log("Result:", result1);
    // Example 2: Execute tool requiring confirmation
    console.log("\n--- Example 2: Tool requiring confirmation ---");
    const result2 = await registry.execute("delete-file", { path: "/src/temp.ts" }, { userId: "user-123", userRole: "admin" });
    console.log("Result:", result2);
    // Example 3: Blocked tool (guest user)
    console.log("\n--- Example 3: Blocked tool ---");
    const result3 = await registry.execute("delete-file", { path: "/src/important.ts" }, { userId: "guest-456", userRole: "guest" });
    console.log("Result:", result3);
    // Example 4: Get all tools for AI SDK
    console.log("\n--- Example 4: Get tools for AI SDK ---");
    const tools = registry.getToolsRecord();
    console.log("Available tools:", Object.keys(tools));
    // Example 5: Get tool metadata
    console.log("\n--- Example 5: Tool metadata ---");
    const metadata = registry.getToolMetadata();
    console.log("Tool metadata:", metadata);
}
// Uncomment to run examples:
// exampleUsage();
//# sourceMappingURL=example.js.map