"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 IntegrationSettingsService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IntegrationSettingsService = void 0;
const common_1 = require("@nestjs/common");
const setting_service_1 = require("../../setting/setting.service");
let IntegrationSettingsService = IntegrationSettingsService_1 = class IntegrationSettingsService {
    constructor(settingService) {
        this.settingService = settingService;
        this.logger = new common_1.Logger(IntegrationSettingsService_1.name);
    }
    async getRuntimeSettings() {
        const values = await this.settingService.getSettingValues([
            'outbox-enabled',
            'outbox-processor-enabled',
            'outbox-startup-drain-enabled',
            'outbox-polling-interval-ms',
            'outbox-idle-polling-interval-ms',
            'outbox-batch-size',
            'outbox-startup-drain-batch-size',
            'outbox-max-attempts',
            'outbox-processing-lease-ms',
            'outbox-retry-base-delay-ms',
            'outbox-dead-letter-enabled',
        ]);
        const pollingIntervalMs = this.getPositiveInt(values['outbox-polling-interval-ms'], 5000, 100, 'outbox-polling-interval-ms');
        let idlePollingIntervalMs = this.getPositiveInt(values['outbox-idle-polling-interval-ms'], 30000, 100, 'outbox-idle-polling-interval-ms');
        if (idlePollingIntervalMs < pollingIntervalMs) {
            this.logger.warn(`Setting outbox-idle-polling-interval-ms (${idlePollingIntervalMs}) is lower than outbox-polling-interval-ms (${pollingIntervalMs}). Using ${pollingIntervalMs}.`);
            idlePollingIntervalMs = pollingIntervalMs;
        }
        return {
            outboxEnabled: this.getBoolean(values['outbox-enabled'], true),
            outboxProcessorEnabled: this.getBoolean(values['outbox-processor-enabled'], true),
            startupDrainEnabled: this.getBoolean(values['outbox-startup-drain-enabled'], true),
            pollingIntervalMs,
            idlePollingIntervalMs,
            batchSize: this.getPositiveInt(values['outbox-batch-size'], 10, 1, 'outbox-batch-size'),
            startupDrainBatchSize: this.getPositiveInt(values['outbox-startup-drain-batch-size'], 50, 1, 'outbox-startup-drain-batch-size'),
            maxAttempts: this.getPositiveInt(values['outbox-max-attempts'], 3, 1, 'outbox-max-attempts'),
            processingLeaseMs: this.getPositiveInt(values['outbox-processing-lease-ms'], 30000, 1000, 'outbox-processing-lease-ms'),
            retryBaseDelayMs: this.getPositiveInt(values['outbox-retry-base-delay-ms'], 1000, 100, 'outbox-retry-base-delay-ms'),
            deadLetterEnabled: this.getBoolean(values['outbox-dead-letter-enabled'], true),
        };
    }
    getPositiveInt(value, fallback, min, slug) {
        const parsed = Number(value);
        if (!Number.isFinite(parsed) || parsed < min) {
            this.logger.warn(`Invalid ${slug} setting value (${String(value)}). Using fallback ${fallback}.`);
            return fallback;
        }
        return Math.floor(parsed);
    }
    getBoolean(value, fallback) {
        if (typeof value === 'boolean') {
            return value;
        }
        if (value === 'true' || value === '1') {
            return true;
        }
        if (value === 'false' || value === '0') {
            return false;
        }
        return fallback;
    }
};
exports.IntegrationSettingsService = IntegrationSettingsService;
exports.IntegrationSettingsService = IntegrationSettingsService = IntegrationSettingsService_1 = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [setting_service_1.SettingService])
], IntegrationSettingsService);
//# sourceMappingURL=integration-settings.service.js.map