"use strict";
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 OutboxProcessorJob_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OutboxProcessorJob = void 0;
const common_1 = require("@nestjs/common");
const integration_settings_service_1 = require("./integration-settings.service");
const outbox_polling_coordinator_1 = require("./outbox-polling.coordinator");
const outbox_notifier_1 = require("./outbox.notifier");
const outbox_processor_1 = require("./outbox.processor");
/**
 * Hybrid background job for outbox processing
 * Combines startup recovery, immediate notification reactions, and periodic polling
 */
let OutboxProcessorJob = OutboxProcessorJob_1 = class OutboxProcessorJob {
    constructor(processor, notifier, coordinator, integrationSettingsService) {
        this.processor = processor;
        this.notifier = notifier;
        this.coordinator = coordinator;
        this.integrationSettingsService = integrationSettingsService;
        this.logger = new common_1.Logger(OutboxProcessorJob_1.name);
    }
    /**
     * Called when module initializes
     * 1. Drain pending events from startup
     * 2. Start hybrid processing loop
     */
    async onModuleInit() {
        const settings = await this.integrationSettingsService.getRuntimeSettings();
        const isEnabled = settings.outboxEnabled;
        if (!isEnabled) {
            this.logger.debug('Outbox integration disabled in settings');
            return;
        }
        // Step 1: Startup drain
        const startupDrainEnabled = settings.startupDrainEnabled;
        if (startupDrainEnabled) {
            this.logger.log('Starting outbox startup drain...');
            try {
                const processed = await this.processor.startupDrain();
                this.logger.log(`Outbox startup drain completed: ${processed} events`);
            }
            catch (error) {
                this.logger.error('Outbox startup drain failed:', error);
            }
        }
        // Step 2: Start hybrid processing loop
        const processorEnabled = settings.outboxProcessorEnabled;
        if (processorEnabled) {
            this.logger.log('Starting outbox hybrid processing loop');
            try {
                await this.coordinator.startProcessingLoop();
            }
            catch (error) {
                this.logger.error('Failed to start outbox processing loop:', error);
            }
        }
        else {
            this.logger.debug('Outbox processor disabled in settings');
        }
    }
    /**
     * Called when module is destroyed
     * Gracefully stop the processing loop
     */
    async onModuleDestroy() {
        this.logger.log('Shutting down outbox processing');
        try {
            await this.coordinator.stopProcessingLoop();
        }
        catch (error) {
            this.logger.error('Error stopping outbox processing loop:', error);
        }
    }
    /**
     * Get current processing statistics
     * Useful for health checks and monitoring
     */
    async getStats() {
        const stats = await this.processor.getStats();
        return Object.assign(Object.assign({}, stats), { pollingLoopRunning: this.coordinator.isRunning() });
    }
};
exports.OutboxProcessorJob = OutboxProcessorJob;
exports.OutboxProcessorJob = OutboxProcessorJob = OutboxProcessorJob_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [outbox_processor_1.OutboxProcessor,
        outbox_notifier_1.OutboxNotifier,
        outbox_polling_coordinator_1.OutboxPollingCoordinator,
        integration_settings_service_1.IntegrationSettingsService])
], OutboxProcessorJob);
//# sourceMappingURL=outbox.processor.job.js.map