// resources/templates.ts — Available spec templates resource
// Returns the list of available spec templates (built-in and custom).
import { readFile } from 'node:fs/promises';
import { glob } from 'glob';
import { t } from '../i18n/index.js';
/**
 * Load built-in templates. These are the default SDD spec types.
 */
function getBuiltInTemplates() {
    return [
        {
            id: 'feature',
            name: 'Feature Spec',
            description: 'Full feature specification using unified spec.md sections',
            type: 'feature',
            sections: [
                'metadata',
                'estimation',
                'interfaces',
                'behavior',
                'happyPath',
                'edgeCases',
                'dataFlow',
                'files',
                'verification',
            ],
            source: 'built-in',
        },
        {
            id: 'refactor',
            name: 'Refactor Spec',
            description: 'Refactoring specification focused on code improvements without behavior changes',
            type: 'refactor',
            sections: ['metadata', 'estimation', 'files', 'behavior', 'verification', 'technicalNotes'],
            source: 'built-in',
        },
        {
            id: 'bugfix',
            name: 'Bugfix Spec',
            description: 'Bug fix specification with root cause analysis and regression tests',
            type: 'bugfix',
            sections: ['metadata', 'estimation', 'behavior', 'edgeCases', 'files', 'verification'],
            source: 'built-in',
        },
        {
            id: 'infra',
            name: 'Infrastructure Spec',
            description: 'Infrastructure change specification (CI/CD, deployment, monitoring)',
            type: 'infra',
            sections: ['metadata', 'estimation', 'dataFlow', 'files', 'verification', 'technicalNotes'],
            source: 'built-in',
        },
        {
            id: 'docs',
            name: 'Documentation Spec',
            description: 'Documentation-only spec for generating or updating project docs',
            type: 'docs',
            sections: ['metadata', 'estimation', 'files', 'verification'],
            source: 'built-in',
        },
        {
            id: 'project',
            name: 'Project Spec',
            description: 'Full project initialization spec covering architecture, setup, and conventions',
            type: 'project',
            sections: [
                'metadata',
                'estimation',
                'interfaces',
                'models',
                'schemaDb',
                'behavior',
                'happyPath',
                'edgeCases',
                'dataFlow',
                'files',
                'verification',
                'technicalNotes',
            ],
            source: 'built-in',
        },
    ];
}
/**
 * Load custom templates from the global data directory.
 */
async function loadCustomTemplates() {
    const templates = [];
    try {
        const files = await glob('data/global/templates/*.json');
        for (const file of files) {
            try {
                const raw = await readFile(file, 'utf-8');
                const tmpl = JSON.parse(raw);
                if (tmpl !== null &&
                    typeof tmpl === 'object' &&
                    'id' in tmpl &&
                    typeof tmpl.id === 'string') {
                    const validated = tmpl;
                    validated.source = 'custom';
                    templates.push(validated);
                }
            }
            catch {
                // Skip malformed template files
            }
        }
    }
    catch {
        // No custom templates — that is fine
    }
    return templates;
}
/**
 * MCP resource handler for planu://templates
 */
export async function handleTemplatesResource() {
    const builtIn = getBuiltInTemplates();
    const custom = await loadCustomTemplates();
    const all = [...builtIn, ...custom];
    return JSON.stringify({
        count: all.length,
        builtIn: builtIn.length,
        custom: custom.length,
        templates: all,
    }, null, 2);
}
export const templatesResourceDefinition = {
    uri: 'planu://templates',
    name: 'Spec Templates',
    description: t('resources.templates.description'),
    mimeType: 'application/json',
};
//# sourceMappingURL=templates.js.map