import { buildCommandContext, } from '../../app/command-context.js';
import { readGlobalOptions } from '../shared/shared.utils.js';
import { Command } from 'commander';
import { generateThinIndex as defaultGenerateThinIndex, } from './thin-index.js';
const DEFAULT_DEPENDENCIES = {
    buildCommandContext,
    generateThinIndex: defaultGenerateThinIndex,
};
async function runIndexInit(context, headSha, mergeBaseSha, dependencies) {
    try {
        const result = await dependencies.generateThinIndex({
            repoRoot: context.cwd,
            headSha,
            mergeBaseSha,
        });
        if (context.json) {
            context.logger.json({
                status: 'ok',
                outputPath: result.outputPath,
                repoName: result.repoName,
                packageManager: result.packageManager,
                entryPointCount: result.entryPointCount,
            });
        }
        else {
            context.logger.info(`Generated ${result.outputPath} (thin index)`);
            context.logger.info(`  Repo: ${result.repoName}`);
            context.logger.info(`  Package Manager: ${result.packageManager}`);
            context.logger.info(`  Entry Points: ${result.entryPointCount} detected`);
        }
        process.exitCode = 0;
    }
    catch (error) {
        const message = error instanceof Error ? error.message : String(error);
        if (context.json) {
            context.logger.json({ status: 'error', message });
        }
        else {
            context.logger.error(message);
        }
        process.exitCode = 1;
    }
}
export function createIndexInitCommand(overrides = {}) {
    const dependencies = {
        ...DEFAULT_DEPENDENCIES,
        ...overrides,
    };
    return new Command('init')
        .description('Generate a thin project-index.md for quick repo orientation')
        .option('--head-sha <sha>', 'Override HEAD SHA')
        .option('--merge-base-sha <sha>', 'Override merge-base SHA')
        .action(async (options, command) => {
        const context = dependencies.buildCommandContext(readGlobalOptions(command));
        await runIndexInit(context, options.headSha, options.mergeBaseSha, dependencies);
    });
}
export function createIndexCommand() {
    return new Command('index')
        .description('OAT index generation commands')
        .addCommand(createIndexInitCommand());
}
