import { resolveConcreteScopes } from '../../shared/shared.utils.js';
export async function runInfoTool(context, name, dependencies) {
    const { logger } = context;
    const scopes = resolveConcreteScopes(context.scope);
    const assetsRoot = await dependencies.resolveAssetsRoot();
    for (const scope of scopes) {
        const scopeRoot = await dependencies.resolveScopeRoot(scope, context.cwd, context.home);
        const tools = await dependencies.scanTools({
            scope,
            scopeRoot,
            assetsRoot,
        });
        const match = tools.find((t) => t.name === name);
        if (!match)
            continue;
        const detail = await dependencies.getToolDetail(match, scopeRoot);
        const toolDetail = { ...match, ...detail };
        if (context.json) {
            logger.json({ tool: toolDetail });
            return { found: true, tool: toolDetail };
        }
        logger.info(`${toolDetail.name}`);
        logger.info(`  Type:        ${toolDetail.type}`);
        logger.info(`  Version:     ${toolDetail.version ?? '-'}`);
        logger.info(`  Pack:        ${toolDetail.pack}`);
        logger.info(`  Scope:       ${toolDetail.scope}`);
        logger.info(`  Status:      ${toolDetail.status}`);
        if (toolDetail.description) {
            logger.info(`  Description: ${toolDetail.description}`);
        }
        if (toolDetail.type === 'skill') {
            logger.info(`  Invocable:   ${toolDetail.userInvocable ? 'yes' : 'no'}`);
            if (toolDetail.argumentHint) {
                logger.info(`  Args:        ${toolDetail.argumentHint}`);
            }
            if (toolDetail.allowedTools) {
                logger.info(`  Tools:       ${toolDetail.allowedTools}`);
            }
        }
        if (toolDetail.status === 'outdated') {
            logger.warn(`  Update available: ${toolDetail.version ?? '?'} -> ${toolDetail.bundledVersion ?? '?'}`);
        }
        return { found: true, tool: toolDetail };
    }
    if (context.json) {
        logger.json({ tool: null, error: `Tool '${name}' not found` });
    }
    else {
        logger.error(`Tool '${name}' not found.`);
    }
    return { found: false, tool: null };
}
