"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cancelAdapter = exports.DefaultCancelAdapter = void 0;
exports.createCancelAdapter = createCancelAdapter;
exports.setCancelAdapter = setCancelAdapter;
class DefaultCancelAdapter {
    constructor() {
        this.cancelledRuns = new Set();
        this.gatewayUrl = process.env['GATEWAY_URL'] ||
            process.env['GATEWAY_BASE_URL'] ||
            'http://localhost:3001';
    }
    async cancelRun(runId, reason) {
        console.log(`🛑 CancelAdapter: Cancelling run ${runId} - Reason: ${reason}`);
        if (this.cancelledRuns.has(runId)) {
            return {
                success: false,
                reason: 'Run already cancelled',
                timestamp: Date.now()
            };
        }
        this.cancelledRuns.add(runId);
        try {
            const cancelUrl = `${this.gatewayUrl}/api/runs/${runId}/cancel`;
            const response = await fetch(cancelUrl, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ reason }),
            });
            if (!response.ok) {
                const errorText = await response.text();
                console.error(`❌ CancelAdapter: Failed to cancel run ${runId}: ${response.status} - ${errorText}`);
                this.cancelledRuns.delete(runId);
                return {
                    success: false,
                    reason: `Gateway returned ${response.status}: ${errorText}`,
                    timestamp: Date.now()
                };
            }
            const result = await response.json().catch(() => ({}));
            console.log(`✅ CancelAdapter: Run ${runId} cancelled successfully`);
            return {
                success: true,
                reason: result?.message || 'Cancellation requested',
                timestamp: Date.now()
            };
        }
        catch (error) {
            this.cancelledRuns.delete(runId);
            const errorMessage = error instanceof Error ? error.message : String(error);
            console.error(`❌ CancelAdapter: Error cancelling run ${runId}:`, errorMessage);
            return {
                success: false,
                reason: `Cancel request failed: ${errorMessage}`,
                timestamp: Date.now()
            };
        }
    }
    isCancelled(runId) {
        return this.cancelledRuns.has(runId);
    }
    getCancelledRuns() {
        return Array.from(this.cancelledRuns);
    }
    clearCancelledRuns() {
        this.cancelledRuns.clear();
    }
}
exports.DefaultCancelAdapter = DefaultCancelAdapter;
function createCancelAdapter() {
    return new DefaultCancelAdapter();
}
let injectedCancelAdapter = null;
function setCancelAdapter(adapter) {
    injectedCancelAdapter = adapter;
}
function resolveCancelAdapter() {
    return injectedCancelAdapter ?? new DefaultCancelAdapter();
}
exports.cancelAdapter = {
    cancelRun(runId, reason) {
        return resolveCancelAdapter().cancelRun(runId, reason);
    },
};
//# sourceMappingURL=cancel-adapter.js.map