/**
 * Tool Installation Metadata
 *
 * Dynamically loads tool definitions from individual JSON files in the tools/ directory.
 * Each JSON file defines installation commands and metadata for a single development tool.
 *
 * To add a new tool, create a JSON file in tools/ matching the ToolMetadata schema:
 *   tools/<tool-name>.json
 */
import { readdirSync, readFileSync } from 'node:fs';
import { join, basename } from 'node:path';
import { fileURLToPath } from 'node:url';
const REQUIRED_FIELDS = [
    'name',
    'summary',
    'description',
    'tags',
    'binary',
    'packageManager',
    'commands',
    'timeout',
    'documentationUrl',
    'verifyCommand',
];
function loadToolMetadata() {
    // NOTE: This module uses import.meta.url to resolve the tools/ directory.
    // It works correctly when loaded in the Node.js CLI bootstrap context but
    // breaks when bundled by Turbopack (import.meta.url points to the chunk
    // location where tools/ doesn't exist). API routes should access tool
    // metadata via the DI container, not by importing this module directly.
    const toolsDir = join(fileURLToPath(import.meta.url), '..', 'tools');
    const metadata = {};
    let files;
    try {
        files = readdirSync(toolsDir).filter((f) => f.endsWith('.json'));
    }
    catch {
        return metadata;
    }
    for (const file of files) {
        const toolName = basename(file, '.json');
        try {
            const raw = readFileSync(join(toolsDir, file), 'utf-8');
            const parsed = JSON.parse(raw);
            const missing = REQUIRED_FIELDS.filter((field) => !(field in parsed));
            if (missing.length > 0) {
                continue;
            }
            metadata[toolName] = parsed;
        }
        catch {
            // Skip malformed JSON files
        }
    }
    return metadata;
}
export const TOOL_METADATA = loadToolMetadata();
/**
 * Returns entries from TOOL_METADATA that can open a directory as an IDE.
 * A tool qualifies if it has an `openDirectory` field, regardless of category.
 * Each entry is [toolId, metadata].
 */
export function getIdeEntries() {
    return Object.entries(TOOL_METADATA).filter(([, meta]) => meta.openDirectory != null);
}
/**
 * Returns entries from TOOL_METADATA tagged as "terminal" that have an
 * `openDirectory` field. Each entry is [toolId, metadata].
 */
export function getTerminalEntries() {
    return Object.entries(TOOL_METADATA).filter(([, meta]) => meta.tags.includes('terminal') && meta.openDirectory != null);
}
