/**
 * Check Agent Auth Use Case
 *
 * Reports the install + auth status of the currently selected AI coding
 * agent. Used by the onboarding empty state to drive the "Ready / Not
 * installed / Needs auth" checklist.
 *
 * Owns:
 *  - Reading the active agent type from settings
 *  - Mapping agent type → user-facing label, tool id, binary name
 *  - Looking up tool installation status via ListToolsUseCase
 *  - Delegating credential detection to IAgentAuthDetectorService
 *
 * Presentation-agnostic: callable from CLI, TUI, and Web identically.
 * The shape returned mirrors what the web UI consumes today; CLI/TUI may
 * project a subset.
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { injectable, inject } from 'tsyringe';
import { AgentType } from '../../../domain/generated/output.js';
import { ListToolsUseCase } from '../tools/list-tools.use-case.js';
/**
 * Co-located metadata table — pure facts about each AgentType. Lives in
 * the use case because it is the only consumer that needs all three pieces
 * (label + tool id + binary) wired together.
 */
const AGENT_METADATA = {
    [AgentType.ClaudeCode]: {
        label: 'Claude Code',
        toolId: 'claude-code',
        binaryName: 'claude',
    },
    [AgentType.Cursor]: { label: 'Cursor Agent', toolId: 'cursor-cli', binaryName: 'cursor-agent' },
    [AgentType.GeminiCli]: { label: 'Gemini CLI', toolId: 'gemini-cli', binaryName: 'gemini' },
    [AgentType.CopilotCli]: { label: 'Copilot CLI', toolId: 'copilot-cli', binaryName: 'copilot' },
    [AgentType.Aider]: { label: 'Aider', toolId: null, binaryName: null },
    [AgentType.Continue]: { label: 'Continue', toolId: null, binaryName: null },
    [AgentType.Dev]: { label: 'Demo', toolId: null, binaryName: null },
    [AgentType.Cline]: { label: 'Cline', toolId: null, binaryName: 'cline' },
    [AgentType.OpenRouter]: { label: 'OpenRouter', toolId: null, binaryName: null },
    [AgentType.TogetherAi]: { label: 'Together AI', toolId: null, binaryName: null },
    [AgentType.Ollama]: { label: 'Ollama', toolId: null, binaryName: null },
};
const UNKNOWN_RESULT = {
    agentType: 'unknown',
    installed: false,
    authenticated: false,
    label: 'Unknown',
    binaryName: null,
    installCommand: null,
    authCommand: null,
};
let CheckAgentAuthUseCase = class CheckAgentAuthUseCase {
    listToolsUseCase;
    authDetector;
    settingsRepository;
    constructor(listToolsUseCase, authDetector, settingsRepository) {
        this.listToolsUseCase = listToolsUseCase;
        this.authDetector = authDetector;
        this.settingsRepository = settingsRepository;
    }
    async execute() {
        let agentType;
        try {
            const settings = await this.settingsRepository.load();
            if (settings === null) {
                return UNKNOWN_RESULT;
            }
            agentType = settings.agent.type;
        }
        catch {
            return UNKNOWN_RESULT;
        }
        const metadata = AGENT_METADATA[agentType];
        if (!metadata) {
            return { ...UNKNOWN_RESULT, agentType };
        }
        // Agents with no associated tool (dev/demo, aider, continue) — assume ready.
        if (!metadata.toolId) {
            return {
                agentType,
                installed: true,
                authenticated: true,
                label: metadata.label,
                binaryName: metadata.binaryName,
                installCommand: null,
                authCommand: null,
            };
        }
        // Resolve installation status from the tool catalogue.
        let installed = false;
        let installCommand = null;
        try {
            const tools = await this.listToolsUseCase.execute();
            const tool = tools.find((t) => t.id === metadata.toolId);
            installed = tool?.status.status === 'available';
            installCommand = tool?.installCommand ?? null;
        }
        catch {
            installed = false;
        }
        if (!installed) {
            return {
                agentType,
                installed: false,
                authenticated: false,
                label: metadata.label,
                binaryName: metadata.binaryName,
                installCommand,
                authCommand: metadata.binaryName ? `Install ${metadata.label} first` : null,
            };
        }
        // Tool is installed — defer credential detection to the platform adapter.
        const authenticated = await this.authDetector.isAuthenticated(agentType, metadata.binaryName);
        return {
            agentType,
            installed: true,
            authenticated,
            label: metadata.label,
            binaryName: metadata.binaryName,
            installCommand,
            authCommand: authenticated ? null : metadata.binaryName,
        };
    }
};
CheckAgentAuthUseCase = __decorate([
    injectable(),
    __param(0, inject(ListToolsUseCase)),
    __param(1, inject('IAgentAuthDetectorService')),
    __param(2, inject('ISettingsRepository')),
    __metadata("design:paramtypes", [ListToolsUseCase, Object, Object])
], CheckAgentAuthUseCase);
export { CheckAgentAuthUseCase };
