import { z } from "zod";
import { createToolFactory, PermissionLevel, ToolCategory } from "../types.js";
/**
 * Glob tool - Fast file pattern matching.
 */
export const globToolFactory = createToolFactory("glob", (services) => ({
    description: `
Fast file pattern matching across the directory tree.

Use this tool when:
- Finding all files of a specific type (e.g., "**/*.tsx" for all React components)
- Searching for files matching patterns across multiple directories
- Locating test files, config files, or any pattern-based file search
- You know what pattern you're looking for but not the exact location

Examples:
- "**/*.test.ts" → Find all test files anywhere in project
- "src/components/**/*.tsx" → Find all React components in components directory
- "**/config.*" → Find all config files

Returns matching file paths and count. Patterns use glob syntax.
`,
    category: ToolCategory.FILE,
    defaultPermission: PermissionLevel.ALLOW,
    inputSchema: z.object({
        pattern: z.string().describe("Glob pattern (e.g., **/*.js)"),
        path: z.string().optional().describe("Directory to search in"),
    }),
    readOnly: true,
    execute: async ({ pattern, path: searchPath }) => {
        try {
            // Prepend search path to pattern if specified
            const fullPattern = searchPath
                ? `${searchPath.replace(/\/$/, "")}/${pattern}`
                : pattern;
            const files = await services.appShell.listFiles([fullPattern], {
                dot: true,
            });
            // If search path was specified, make paths relative to it
            const resultFiles = searchPath
                ? files
                    .filter((f) => f.startsWith(searchPath.replace(/\/$/, "")))
                    .map((f) => f.substring(searchPath.replace(/\/$/, "").length + 1))
                : files;
            return {
                files: resultFiles,
                count: resultFiles.length,
            };
        }
        catch (error) {
            if (error instanceof Error) {
                throw new Error(`Failed to glob files: ${error.message}`);
            }
            throw new Error("Failed to glob files: Unknown error");
        }
    },
    getSummary: (input, output) => {
        const count = output.count;
        const pattern = input.pattern;
        const pathInfo = input.path ? ` in ${input.path}` : "";
        return {
            input: `pattern: "${pattern}"${pathInfo}`,
            output: count === 0
                ? "No matching files"
                : `Found ${count} file${count === 1 ? "" : "s"}`,
        };
    },
}));
//# sourceMappingURL=glob.js.map