/**
 * Agent installation functions.
 * Ports lib/skills/agents-install.sh.
 *
 * Installs agent configurations to the appropriate locations.
 *
 * @epic T4454
 * @task T4518
 */
import { existsSync, mkdirSync, readdirSync, readlinkSync, symlinkSync, unlinkSync } from 'node:fs';
import { platform } from 'node:os';
import { basename, join } from 'node:path';
import { getClaudeAgentsDir } from '../../paths.js';
import { getAgentsDir } from './config.js';
/**
 * Symlink type for directory symlinks.
 * On Windows, use 'junction' (no admin privileges required).
 * On Unix, use 'dir'.
 */
const DIR_SYMLINK_TYPE = platform() === 'win32' ? 'junction' : 'dir';
// ============================================================================
// Agent Installation
// ============================================================================
/** Installation target directory. */
function getAgentInstallDir() {
    return getClaudeAgentsDir();
}
/**
 * Install a single agent via symlink.
 * @task T4518
 */
export function installAgent(agentDir) {
    const targetDir = getAgentInstallDir();
    const agentName = basename(agentDir);
    const targetPath = join(targetDir, agentName);
    // Ensure target directory exists
    if (!existsSync(targetDir)) {
        mkdirSync(targetDir, { recursive: true });
    }
    // Check source exists
    if (!existsSync(agentDir)) {
        return { installed: false, path: targetPath, error: `Source not found: ${agentDir}` };
    }
    // Handle existing entry
    if (existsSync(targetPath)) {
        try {
            const existing = readlinkSync(targetPath);
            if (existing === agentDir) {
                return { installed: true, path: targetPath }; // Already correct
            }
            // Different target, remove and re-link
            unlinkSync(targetPath);
        }
        catch {
            // Not a symlink, skip
            return {
                installed: false,
                path: targetPath,
                error: `Target exists and is not a symlink: ${targetPath}`,
            };
        }
    }
    try {
        symlinkSync(agentDir, targetPath, DIR_SYMLINK_TYPE);
        return { installed: true, path: targetPath };
    }
    catch (err) {
        return { installed: false, path: targetPath, error: `Symlink failed: ${err}` };
    }
}
/**
 * Install all agents from the project agents/ directory.
 * @task T4518
 */
export function installAllAgents(cwd) {
    const agentsDir = getAgentsDir(cwd);
    const results = [];
    if (!existsSync(agentsDir)) {
        return results;
    }
    const entries = readdirSync(agentsDir);
    for (const entry of entries) {
        if (entry.startsWith('.'))
            continue;
        const agentDir = join(agentsDir, entry);
        const agentMdPath = join(agentDir, 'AGENT.md');
        if (!existsSync(agentMdPath))
            continue;
        const result = installAgent(agentDir);
        results.push({
            name: entry,
            installed: result.installed,
            error: result.error,
        });
    }
    return results;
}
/**
 * Uninstall a single agent by removing its symlink.
 * @task T4518
 */
export function uninstallAgent(agentName) {
    const targetDir = getAgentInstallDir();
    const targetPath = join(targetDir, agentName);
    if (!existsSync(targetPath)) {
        return false;
    }
    try {
        unlinkSync(targetPath);
        return true;
    }
    catch {
        return false;
    }
}
//# sourceMappingURL=install.js.map