import { z } from 'zod';
import { config } from '../../config.js';
import { detectProject } from '../../lib/frameworks/detect.js';
import { resolveTarget } from '../../lib/frameworks/resolve.js';
import { addDependencyCommandFor, detectJsPackageManager } from '../../lib/frameworks/packageManager.js';
import { runCommand } from '../../lib/runCommand.js';
import { errorResult, jsonResult, targetSchema, toErrorMessage } from '../shared.js';
export function registerAddDependencyTool(server) {
    server.registerTool('add_dependency', {
        title: 'Add / Remove Dependency',
        description: 'Add or remove a dependency, auto-detecting npm/pnpm/yarn/bun (package.json) vs. Composer (composer.json) from the resolved project. Pass `ecosystem` explicitly to override the auto-detection.',
        inputSchema: {
            packages: z.array(z.string().min(1)).min(1).describe('Package names to add/remove, e.g. ["axios"] or ["laravel/sanctum"].'),
            ecosystem: z.enum(['npm', 'composer']).optional(),
            dev: z.boolean().optional().default(false).describe('Install as a dev dependency (ignored for composer remove).'),
            remove: z.boolean().optional().default(false),
            target: targetSchema,
        },
    }, async ({ packages, ecosystem, dev, remove, target }) => {
        try {
            const detected = detectProject(config.workspaceRoot);
            const { adapter, target: resolvedTarget } = resolveTarget(detected, target);
            const resolvedEcosystem = ecosystem ?? (adapter.packageManager === 'composer' ? 'composer' : 'npm');
            const { command, args } = resolvedEcosystem === 'composer'
                ? {
                    command: 'composer',
                    args: remove
                        ? ['remove', ...packages]
                        : ['require', ...(dev ? ['--dev'] : []), ...packages],
                }
                : addDependencyCommandFor(detectJsPackageManager(adapter.root), packages, { dev, remove });
            const result = await runCommand(command, args, {
                cwd: adapter.root,
                timeoutMs: config.scaffoldCommandTimeoutMs,
            });
            return jsonResult({ target: resolvedTarget, ecosystem: resolvedEcosystem, ...result });
        }
        catch (error) {
            return errorResult(toErrorMessage(error));
        }
    });
}
//# sourceMappingURL=addDependency.js.map