import { existsSync } from 'node:fs';
import { join, relative } from 'node:path';
import { composeContent, generateHeader } from '../domain/composer.js';
import { readContextFile } from '../domain/reader.js';
import { generateSkillsIndex } from '../domain/skills-indexer.js';
import { computeStats, groupFiles, mergeSkillDirs, scanMdFiles, } from '../domain/skills-scanner.js';
import { PROVIDER_CONFIGS } from '../domain/types.js';
import { BaseAdapter } from './base.adapter.js';
export class ClaudeAdapter extends BaseAdapter {
    providerName = 'claude';
    generate(context, rootDir) {
        const config = PROVIDER_CONFIGS.claude;
        const template = readContextFile(context);
        const composed = composeContent(template, config);
        const contextRelPath = relative(rootDir, context.contextFile);
        const header = generateHeader(contextRelPath, this.providerName);
        // Build skills index
        let skillsIndex = '';
        if (context.isRoot && context.hasSkills) {
            const files = scanMdFiles(join(context.aiDir, 'skills'), context.aiDir);
            const agentFiles = context.hasAgents
                ? scanMdFiles(join(context.aiDir, 'agents'), context.aiDir)
                : [];
            const commandFiles = context.hasCommands
                ? scanMdFiles(join(context.aiDir, 'commands'), context.aiDir)
                : [];
            const allFiles = [...files, ...agentFiles, ...commandFiles];
            const groups = groupFiles(allFiles);
            computeStats(groups);
            skillsIndex = '\n' + generateSkillsIndex(groups, config.skillsDir) + '\n';
        }
        else if (!context.isRoot) {
            const rootAiDir = join(rootDir, '.ai');
            const contextSkillsDir = join(context.aiDir, 'skills');
            const rootSkillsDir = join(rootAiDir, 'skills');
            const mergedGroups = mergeSkillDirs(contextSkillsDir, rootSkillsDir);
            const contextAgentFiles = context.hasAgents
                ? scanMdFiles(join(context.aiDir, 'agents'), context.aiDir)
                : [];
            const contextCommandFiles = context.hasCommands
                ? scanMdFiles(join(context.aiDir, 'commands'), context.aiDir)
                : [];
            const rootAgentFiles = existsSync(join(rootAiDir, 'agents'))
                ? scanMdFiles(join(rootAiDir, 'agents'), rootAiDir)
                : [];
            const rootCommandFiles = existsSync(join(rootAiDir, 'commands'))
                ? scanMdFiles(join(rootAiDir, 'commands'), rootAiDir)
                : [];
            const extraFiles = [
                ...contextAgentFiles,
                ...contextCommandFiles,
                ...rootAgentFiles,
                ...rootCommandFiles,
            ];
            const extraGroups = groupFiles(extraFiles);
            const allGroups = [...mergedGroups, ...extraGroups];
            if (allGroups.length > 0) {
                skillsIndex =
                    '\n' + generateSkillsIndex(allGroups, config.skillsDir) + '\n';
            }
        }
        const outputPath = join(context.contextDir, config.instructionFile);
        const content = header + composed.trimEnd() + '\n' + skillsIndex;
        return [{ content, path: outputPath, type: 'instruction' }];
    }
}
