/**
 * SyncEngine — Unified sync orchestrator
 *
 * ADR-0234: Single SyncEngine replaces 17+ scattered modules.
 * ADR-0235: ProviderAdapter interface for clean provider separation.
 *
 * Entry points: push(), pull(), reconcile()
 * Each provider (GitHub, JIRA, ADO) implements ProviderAdapter.
 */
const PRIORITY_COLORS = {
    P0: 'b60205', // red
    P1: 'd93f0b', // orange-red
    P2: 'fbca04', // yellow
    P3: '0e8a16', // green
};
const TYPE_COLORS = {
    'user-story': 'e4e669',
    task: 'bfdadc',
    feature: '5319e7',
    bug: 'd73a4a',
};
/**
 * Generate standardized labels for an issue.
 * Replaces the old system that labeled everything "critical".
 */
export function generateLabels(config) {
    const labels = [];
    // Priority label
    labels.push({
        name: `priority:${config.priority}`,
        color: PRIORITY_COLORS[config.priority] || 'c5def5',
    });
    // Type label
    labels.push({
        name: `type:${config.type}`,
        color: TYPE_COLORS[config.type] || 'c5def5',
    });
    // Project label (single format, no duplicates)
    labels.push({
        name: `project:${config.projectName}`,
        color: '0075ca',
    });
    return labels;
}
export class SyncEngine {
    constructor(config) {
        this.providers = new Map();
        this.permissions = config.permissions;
    }
    registerProvider(adapter) {
        this.providers.set(adapter.platform, adapter);
    }
    getProvider(platform) {
        return this.providers.get(platform);
    }
    getRegisteredPlatforms() {
        return [...this.providers.keys()];
    }
    // -------------------------------------------------------------------------
    // Connection
    // -------------------------------------------------------------------------
    async testConnection(platform) {
        const adapter = this.requireProvider(platform);
        return adapter.testConnection();
    }
    // -------------------------------------------------------------------------
    // Push (SpecWeave → External)
    // -------------------------------------------------------------------------
    async push(platform, request) {
        this.requirePermission('canUpsert', 'push');
        const adapter = this.requireProvider(platform);
        const ref = await adapter.createIssue(request.userStory, request.feature);
        return { ref, action: 'created' };
    }
    // -------------------------------------------------------------------------
    // Pull (External → SpecWeave)
    // -------------------------------------------------------------------------
    async pull(platform, since) {
        this.requirePermission('canRead', 'pull');
        const adapter = this.requireProvider(platform);
        const changes = await adapter.pullChanges(since);
        return { changes, platform };
    }
    // -------------------------------------------------------------------------
    // Update & Close
    // -------------------------------------------------------------------------
    async updateIssue(ref, changes) {
        this.requirePermission('canUpsert', 'update');
        const adapter = this.requireProvider(ref.platform);
        return adapter.updateIssue(ref, changes);
    }
    async closeIssue(ref, comment) {
        this.requirePermission('canUpdateStatus', 'close');
        const adapter = this.requireProvider(ref.platform);
        return adapter.closeIssue(ref, comment);
    }
    // -------------------------------------------------------------------------
    // Reconcile
    // -------------------------------------------------------------------------
    async reconcile(platform, items) {
        this.requirePermission('canUpdateStatus', 'reconcile');
        const adapter = this.requireProvider(platform);
        return adapter.reconcile(items);
    }
    // -------------------------------------------------------------------------
    // Hierarchy detection
    // -------------------------------------------------------------------------
    async detectHierarchy(platform) {
        this.requirePermission('canRead', 'detectHierarchy');
        const adapter = this.requireProvider(platform);
        return adapter.detectHierarchy();
    }
    // -------------------------------------------------------------------------
    // Private helpers
    // -------------------------------------------------------------------------
    requireProvider(platform) {
        const adapter = this.providers.get(platform);
        if (!adapter) {
            throw new Error(`Provider "${platform}" is not registered. Register it with engine.registerProvider().`);
        }
        return adapter;
    }
    requirePermission(key, operation) {
        if (!this.permissions[key]) {
            throw new Error(`Permission denied: "${key}" is required for ${operation} operation. Check your sync preset/permissions.`);
        }
    }
}
//# sourceMappingURL=engine.js.map