/**
 * CancelationHandler - Graceful Ctrl+C (SIGINT) handling with state persistence
 *
 * Features:
 * - Single Ctrl+C: Save state and exit gracefully
 * - Double Ctrl+C (within 2s): Force exit immediately
 * - Polling mechanism for cooperative cancelation
 * - State persistence to resume later
 *
 * @module core/progress/cancelation-handler
 */
import { consoleLogger } from '../../utils/logger.js';
/**
 * CancelationHandler - Handles SIGINT gracefully with double Ctrl+C force exit
 *
 * @example
 * ```typescript
 * const handler = new CancelationHandler({
 *   onSaveState: async () => {
 *     await saveImportState();
 *   }
 * });
 *
 * handler.register();
 *
 * // In import loop
 * for (const project of projects) {
 *   if (handler.shouldCancel()) {
 *     break;
 *   }
 *   await importProject(project);
 * }
 *
 * handler.unregister();
 * ```
 */
export class CancelationHandler {
    constructor(options = {}) {
        this.cancelRequested = false;
        this.firstCtrlCTime = null;
        this.sigintHandler = null;
        this.logger = options.logger ?? consoleLogger;
        this.onSaveState = options.onSaveState;
        this.forceExitTimeout = options.forceExitTimeout ?? 2000;
    }
    /**
     * Register SIGINT listener
     */
    register() {
        this.sigintHandler = () => {
            void this.handleSigint();
        };
        process.on('SIGINT', this.sigintHandler);
    }
    /**
     * Unregister SIGINT listener
     */
    unregister() {
        if (this.sigintHandler) {
            process.off('SIGINT', this.sigintHandler);
            this.sigintHandler = null;
        }
    }
    /**
     * Check if cancelation was requested (polling mechanism)
     *
     * @returns true if Ctrl+C was pressed
     */
    shouldCancel() {
        return this.cancelRequested;
    }
    /**
     * Handle SIGINT signal (Ctrl+C)
     */
    async handleSigint() {
        const now = Date.now();
        // Double Ctrl+C detection
        if (this.firstCtrlCTime !== null) {
            const elapsed = now - this.firstCtrlCTime;
            if (elapsed < this.forceExitTimeout) {
                // Force exit on double Ctrl+C
                this.logger.warn('\n⚠️  Forced exit (progress may be lost)');
                process.exit(1);
            }
        }
        // First Ctrl+C
        this.firstCtrlCTime = now;
        this.cancelRequested = true;
        this.logger.log('\n⏸️  Cancelation requested. Saving progress...');
        try {
            // Save state if callback provided
            if (this.onSaveState) {
                await this.onSaveState();
            }
            this.logger.log('✅ Progress saved. Run with --resume to continue.');
            process.exit(0);
        }
        catch (error) {
            this.logger.error('❌ Failed to save progress:', error);
            process.exit(1);
        }
    }
    /**
     * Reset cancelation state (useful for testing)
     */
    reset() {
        this.cancelRequested = false;
        this.firstCtrlCTime = null;
    }
}
//# sourceMappingURL=cancelation-handler.js.map