import { existsSync, readdirSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { homedir } from 'node:os';
import { logDebug } from '../utils/debugLogger.js';
const dataRoot = process.env['AGI_DATA_DIR']?.trim() || join(homedir(), '.agi');
const defaultCommandsDir = process.env['AGI_COMMANDS_DIR']?.trim() || join(dataRoot, 'commands');
export function loadCustomSlashCommands(dir = defaultCommandsDir) {
    if (!existsSync(dir)) {
        return [];
    }
    const files = readdirSync(dir).filter((file) => file.toLowerCase().endsWith('.json'));
    const commands = [];
    const seen = new Set();
    for (const file of files) {
        const filePath = join(dir, file);
        try {
            const raw = readFileSync(filePath, 'utf8');
            const parsed = JSON.parse(raw);
            if (Array.isArray(parsed)) {
                for (const entry of parsed) {
                    const cmd = normalizeCommand(entry, filePath);
                    if (cmd && !seen.has(cmd.command)) {
                        seen.add(cmd.command);
                        commands.push(cmd);
                    }
                }
            }
            else {
                const cmd = normalizeCommand(parsed, filePath);
                if (cmd && !seen.has(cmd.command)) {
                    seen.add(cmd.command);
                    commands.push(cmd);
                }
            }
        }
        catch (error) {
            logDebug(`[custom commands] Failed to load ${filePath}:`, error);
        }
    }
    return commands;
}
export function buildCustomCommandPrompt(command, input, context) {
    const replacements = {
        input,
        workspace: context.workspace,
        profile: context.profile,
        provider: context.provider,
        model: context.model,
    };
    return command.template.replace(/\{\{\s*(\w+)\s*\}\}/g, (_, key) => {
        const normalized = key.toLowerCase();
        return replacements[normalized] ?? replacements[key] ?? '';
    });
}
function normalizeCommand(entry, source) {
    if (!entry || typeof entry !== 'object') {
        return null;
    }
    const record = entry;
    const rawCommandValue = record['command'];
    const rawCommand = typeof rawCommandValue === 'string' ? rawCommandValue.trim() : '';
    const command = rawCommand ? (rawCommand.startsWith('/') ? rawCommand : `/${rawCommand}`) : '';
    const descriptionValue = record['description'];
    const description = typeof descriptionValue === 'string' && descriptionValue.trim()
        ? descriptionValue.trim()
        : 'Custom command';
    const templateEntry = record['template'] ?? record['prompt'];
    const templateValue = typeof templateEntry === 'string' ? templateEntry : '';
    if (!command || !templateValue.trim()) {
        return null;
    }
    const requireInputEntry = record['requireInput'] ?? record['inputRequired'];
    const requireInput = typeof requireInputEntry === 'boolean' ? requireInputEntry : false;
    const categoryEntry = record['category'];
    const category = typeof categoryEntry === 'string' ? categoryEntry.trim() : undefined;
    return {
        command,
        description,
        template: templateValue,
        requireInput,
        source,
        category,
    };
}
//# sourceMappingURL=customCommands.js.map