/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import fs from 'fs';
import path from 'path';
import { glob, escape } from 'glob';
import { BaseDeclarativeTool, BaseToolInvocation, Kind } from './tools.js';
import { shortenPath, makeRelative } from '../utils/paths.js';
import { ToolErrorType } from './tool-error.js';
/**
 * Sorts file entries based on recency and then alphabetically.
 * Recent files (modified within recencyThresholdMs) are listed first, newest to oldest.
 * Older files are listed after recent ones, sorted alphabetically by path.
 */
export function sortFileEntries(entries, nowTimestamp, recencyThresholdMs) {
    const sortedEntries = [...entries];
    sortedEntries.sort((a, b) => {
        const mtimeA = a.mtimeMs ?? 0;
        const mtimeB = b.mtimeMs ?? 0;
        const aIsRecent = nowTimestamp - mtimeA < recencyThresholdMs;
        const bIsRecent = nowTimestamp - mtimeB < recencyThresholdMs;
        if (aIsRecent && bIsRecent) {
            return mtimeB - mtimeA;
        }
        else if (aIsRecent) {
            return -1;
        }
        else if (bIsRecent) {
            return 1;
        }
        else {
            return a.fullpath().localeCompare(b.fullpath());
        }
    });
    return sortedEntries;
}
class GlobToolInvocation extends BaseToolInvocation {
    config;
    constructor(config, params) {
        super(params);
        this.config = config;
    }
    getDescription() {
        let description = `'${this.params.pattern}'`;
        if (this.params.path) {
            const searchDir = path.resolve(this.config.getTargetDir(), this.params.path || '.');
            const relativePath = makeRelative(searchDir, this.config.getTargetDir());
            description += ` within ${shortenPath(relativePath)}`;
        }
        return description;
    }
    async execute(signal) {
        try {
            // Get ephemeral settings for output limits
            const ephemeralSettings = this.config.getEphemeralSettings();
            const maxItems = ephemeralSettings['tool-output-max-items'] ??
                50;
            const workspaceContext = this.config.getWorkspaceContext();
            const workspaceDirectories = workspaceContext.getDirectories();
            // If a specific path is provided, resolve it and check if it's within workspace
            let searchDirectories;
            if (this.params.path) {
                const searchDirAbsolute = path.resolve(this.config.getTargetDir(), this.params.path);
                if (!workspaceContext.isPathWithinWorkspace(searchDirAbsolute)) {
                    const rawError = `Error: Path "${this.params.path}" is not within any workspace directory`;
                    return {
                        llmContent: rawError,
                        returnDisplay: `Path is not within workspace`,
                        error: {
                            message: rawError,
                            type: ToolErrorType.PATH_NOT_IN_WORKSPACE,
                        },
                    };
                }
                searchDirectories = [searchDirAbsolute];
            }
            else {
                // Search across all workspace directories
                searchDirectories = workspaceDirectories;
            }
            // Get centralized file discovery service
            const respectGitIgnore = this.params.respect_git_ignore ??
                this.config.getFileFilteringRespectGitIgnore();
            const fileDiscovery = this.config.getFileService();
            // Collect entries from all search directories
            let allEntries = [];
            for (const searchDir of searchDirectories) {
                let pattern = this.params.pattern;
                const fullPath = path.join(searchDir, pattern);
                if (fs.existsSync(fullPath)) {
                    pattern = escape(pattern);
                }
                const entries = (await glob(pattern, {
                    cwd: searchDir,
                    withFileTypes: true,
                    nodir: true,
                    stat: true,
                    nocase: !this.params.case_sensitive,
                    dot: true,
                    ignore: this.config.getFileExclusions().getGlobExcludes(),
                    follow: false,
                    signal,
                }));
                allEntries = allEntries.concat(entries);
            }
            const entries = allEntries;
            // Apply git-aware filtering if enabled and in git repository
            let filteredEntries = entries;
            let gitIgnoredCount = 0;
            if (respectGitIgnore) {
                const toCanonicalPath = (filePath) => {
                    try {
                        return fs.realpathSync(filePath);
                    }
                    catch (_error) {
                        return path.normalize(filePath);
                    }
                };
                const canonicalPaths = entries.map((entry) => toCanonicalPath(entry.fullpath()));
                const filteredCanonicalPaths = new Set(fileDiscovery
                    .filterFiles(canonicalPaths, {
                    respectGitIgnore,
                    respectLlxprtIgnore: false,
                })
                    .map((p) => toCanonicalPath(p)));
                filteredEntries = entries.filter((entry) => filteredCanonicalPaths.has(toCanonicalPath(entry.fullpath())));
                gitIgnoredCount = entries.length - filteredEntries.length;
            }
            if (!filteredEntries || filteredEntries.length === 0) {
                let message = `No files found matching pattern "${this.params.pattern}"`;
                if (searchDirectories.length === 1) {
                    message += ` within ${searchDirectories[0]}`;
                }
                else {
                    message += ` within ${searchDirectories.length} workspace directories`;
                }
                if (gitIgnoredCount > 0) {
                    message += ` (${gitIgnoredCount} files were git-ignored)`;
                }
                return {
                    llmContent: message,
                    returnDisplay: `No files found`,
                };
            }
            // Set filtering such that we first show the most recent files
            const oneDayInMs = 24 * 60 * 60 * 1000;
            const nowTimestamp = new Date().getTime();
            // Sort the filtered entries using the new helper function
            const sortedEntries = sortFileEntries(filteredEntries, nowTimestamp, oneDayInMs);
            const sortedAbsolutePaths = sortedEntries.map((entry) => entry.fullpath());
            const totalFileCount = sortedAbsolutePaths.length;
            let fileListToShow = sortedAbsolutePaths;
            let truncatedMessage = '';
            // Apply max items limit
            if (totalFileCount > maxItems) {
                fileListToShow = sortedAbsolutePaths.slice(0, maxItems);
                truncatedMessage = `\n\n**Note: Output limited to ${maxItems} files out of ${totalFileCount} total matches. Use more specific patterns or adjust 'tool-output-max-items' setting to see more.**`;
            }
            const fileListDescription = fileListToShow.join('\n');
            let resultMessage = `Found ${totalFileCount} file(s) matching "${this.params.pattern}"`;
            if (searchDirectories.length === 1) {
                resultMessage += ` within ${searchDirectories[0]}`;
            }
            else {
                resultMessage += ` across ${searchDirectories.length} workspace directories`;
            }
            if (gitIgnoredCount > 0) {
                resultMessage += ` (${gitIgnoredCount} additional files were git-ignored)`;
            }
            if (totalFileCount > 0) {
                resultMessage += `, sorted by modification time (newest first):\n${fileListDescription}`;
            }
            resultMessage += truncatedMessage;
            return {
                llmContent: resultMessage,
                returnDisplay: `Found ${totalFileCount} matching file(s)`,
            };
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : String(error);
            console.error(`GlobLogic execute Error: ${errorMessage}`, error);
            const rawError = `Error during glob search operation: ${errorMessage}`;
            return {
                llmContent: rawError,
                returnDisplay: `Error: An unexpected error occurred.`,
                error: {
                    message: rawError,
                    type: ToolErrorType.GLOB_EXECUTION_ERROR,
                },
            };
        }
    }
}
/**
 * Implementation of the Glob tool logic
 */
export class GlobTool extends BaseDeclarativeTool {
    config;
    static Name = 'glob';
    constructor(config, _messageBus) {
        super(GlobTool.Name, 'FindFiles', 'Efficiently finds files matching specific glob patterns (e.g., `src/**/*.ts`, `**/*.md`), returning absolute paths sorted by modification time (newest first). Ideal for quickly locating files based on their name or path structure, especially in large codebases.', Kind.Search, {
            properties: {
                pattern: {
                    description: "The glob pattern to match against (e.g., '**/*.py', 'docs/*.md').",
                    type: 'string',
                },
                path: {
                    description: 'Optional: The absolute path to the directory to search within. If omitted, searches the root directory.',
                    type: 'string',
                },
                case_sensitive: {
                    description: 'Optional: Whether the search should be case-sensitive. Defaults to false.',
                    type: 'boolean',
                },
                respect_git_ignore: {
                    description: 'Optional: Whether to respect .gitignore patterns when finding files. Only available in git repositories. Defaults to true.',
                    type: 'boolean',
                },
            },
            required: ['pattern'],
            type: 'object',
        });
        this.config = config;
    }
    /**
     * Validates the parameters for the tool.
     */
    validateToolParamValues(params) {
        const searchDirAbsolute = path.resolve(this.config.getTargetDir(), params.path || '.');
        const workspaceContext = this.config.getWorkspaceContext();
        if (!workspaceContext.isPathWithinWorkspace(searchDirAbsolute)) {
            const directories = workspaceContext.getDirectories();
            return `Search path ("${searchDirAbsolute}") resolves outside the allowed workspace directories: ${directories.join(', ')}`;
        }
        const targetDir = searchDirAbsolute || this.config.getTargetDir();
        try {
            if (!fs.existsSync(targetDir)) {
                return `Search path does not exist ${targetDir}`;
            }
            if (!fs.statSync(targetDir).isDirectory()) {
                return `Search path is not a directory: ${targetDir}`;
            }
        }
        catch (e) {
            return `Error accessing search path: ${e}`;
        }
        if (!params.pattern ||
            typeof params.pattern !== 'string' ||
            params.pattern.trim() === '') {
            return "The 'pattern' parameter cannot be empty.";
        }
        return null;
    }
    createInvocation(params, _messageBus) {
        return new GlobToolInvocation(this.config, params);
    }
}
//# sourceMappingURL=glob.js.map