import fs from 'node:fs/promises';
import path from 'node:path';
import { parseAcodeCommandMarkdown } from './commandParser.js';
async function pathExists(targetPath) {
    try {
        await fs.access(targetPath);
        return true;
    }
    catch {
        return false;
    }
}
/**
 * 从命令目录加载 Markdown slash commands。
 * 主流程：跳过不存在目录与隐藏文件 -> 解析 `.md` 文件 -> 按文件名稳定排序。
 */
export async function loadAcodeCommands(commandDirs) {
    const commands = [];
    for (const commandDir of commandDirs) {
        if (!(await pathExists(commandDir))) {
            continue;
        }
        const entries = await fs.readdir(commandDir, { withFileTypes: true });
        for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
            if (!entry.isFile() || entry.name.startsWith('.') || path.extname(entry.name).toLowerCase() !== '.md') {
                continue;
            }
            const sourcePath = path.join(commandDir, entry.name);
            commands.push(parseAcodeCommandMarkdown(sourcePath, await fs.readFile(sourcePath, 'utf-8')));
        }
    }
    return commands;
}
