/**
 * ADO Client Factory
 *
 * Creates profile-aware ADO clients for multi-project sync support.
 * DELEGATES to AdoProfileResolver (plugin) to determine which org/project to use
 * based on the increment's metadata.
 *
 * This is the FIX for the single-defaultProfile problem:
 * - Each increment can have its own ADO profile in metadata.json
 * - The factory resolves the profile and creates a client for that specific org/project
 *
 * @module integrations/ado/ado-client-factory
 */
import { AdoClient } from './ado-client.js';
import { getAdoPat } from './ado-pat-provider.js';
import { consoleLogger } from '../../utils/logger.js';
import { AdoProfileResolver, } from '../../../plugins/specweave/lib/integrations/ado/ado-profile-resolver.js';
/**
 * ADO Client Factory
 *
 * Resolves the correct ADO profile for an increment and creates
 * a client configured for that specific ADO project.
 *
 * Uses AdoProfileResolver from the plugin (single source of truth).
 */
export class AdoClientFactory {
    constructor(options = {}) {
        this.projectRoot = options.projectRoot ?? process.cwd();
        this.logger = options.logger ?? consoleLogger;
        this.resolver = new AdoProfileResolver(this.projectRoot);
    }
    /**
     * Create an ADO client for a specific increment
     *
     * Resolution priority:
     * 1. Increment's metadata.json -> external_sync.ado.profile
     * 2. Global config.json -> sync.defaultProfile
     *
     * @param incrementId - The increment ID (e.g., "0005-feature-name")
     * @returns Configured AdoClient for the resolved profile
     * @throws Error if no profile found or profile is invalid
     */
    async createClientForIncrement(incrementId) {
        const result = await this.resolveProfile(incrementId);
        if (!result.success || !result.profile) {
            throw new Error(result.error || 'Failed to resolve ADO profile');
        }
        this.logger.log(`🔗 ADO profile resolved: ${result.profile.profileName} ` +
            `(${result.profile.organization}/${result.profile.project}) ` +
            `[source: ${result.source}]`);
        // Get PAT from environment (supports org-specific PATs)
        const pat = getAdoPat(result.profile.organization);
        const config = {
            pat,
            organization: result.profile.organization,
            project: result.profile.project,
        };
        return new AdoClient(config);
    }
    /**
     * Resolve the ADO profile for an increment
     *
     * DELEGATES to AdoProfileResolver from the plugin (single source of truth)
     *
     * @param incrementId - The increment ID
     * @returns Profile resolution result with success/error info
     */
    async resolveProfile(incrementId) {
        return this.resolver.resolveProfile(incrementId);
    }
    /**
     * Set increment's ADO profile
     *
     * DELEGATES to AdoProfileResolver from the plugin
     *
     * @param incrementId - Increment ID
     * @param profileName - Profile name to set
     */
    async setIncrementProfile(incrementId, profileName) {
        await this.resolver.setIncrementProfile(incrementId, profileName);
        this.logger.log(`✅ Set ADO profile "${profileName}" for increment ${incrementId}`);
    }
    /**
     * List available ADO profiles from config
     *
     * DELEGATES to AdoProfileResolver from the plugin
     */
    async listAdoProfiles() {
        return this.resolver.listAdoProfiles();
    }
}
/**
 * Create an ADO client for a specific increment (convenience function)
 *
 * @param incrementId - The increment ID
 * @param projectRoot - Project root directory
 * @returns Configured AdoClient for the resolved profile
 */
export async function createAdoClientForIncrement(incrementId, projectRoot = process.cwd()) {
    const factory = new AdoClientFactory({ projectRoot });
    return factory.createClientForIncrement(incrementId);
}
/**
 * Resolve ADO profile for an increment without creating a client
 *
 * @param incrementId - The increment ID
 * @param projectRoot - Project root directory
 * @returns Profile resolution result
 */
export async function resolveAdoProfileForIncrement(incrementId, projectRoot = process.cwd()) {
    const factory = new AdoClientFactory({ projectRoot });
    return factory.resolveProfile(incrementId);
}
export default AdoClientFactory;
//# sourceMappingURL=ado-client-factory.js.map