"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SnapshotPruningService = void 0;
const backend_common_1 = require("@n8n/backend-common");
const config_1 = require("@n8n/config");
const decorators_1 = require("@n8n/decorators");
const di_1 = require("@n8n/di");
const typeorm_1 = require("@n8n/typeorm");
const n8n_core_1 = require("n8n-core");
const instance_ai_workflow_snapshot_repository_1 = require("./repositories/instance-ai-workflow-snapshot.repository");
let SnapshotPruningService = class SnapshotPruningService {
    constructor(logger, config, snapshotRepo, instanceSettings) {
        this.logger = logger;
        this.config = config;
        this.snapshotRepo = snapshotRepo;
        this.instanceSettings = instanceSettings;
        this.logger = this.logger.scoped('instance-ai');
    }
    init() {
        if (this.instanceSettings.isLeader)
            this.startPruning();
    }
    startPruning() {
        if (this.config.snapshotPruneInterval <= 0)
            return;
        this.pruningInterval = setInterval(async () => await this.prune(), this.config.snapshotPruneInterval);
        this.logger.debug('Started snapshot pruning timer', {
            pruneIntervalMs: this.config.snapshotPruneInterval,
            retentionMs: this.config.snapshotRetention,
        });
    }
    stopPruning() {
        if (this.pruningInterval) {
            clearInterval(this.pruningInterval);
            this.pruningInterval = undefined;
            this.logger.debug('Stopped snapshot pruning timer');
        }
    }
    shutdown() {
        this.stopPruning();
    }
    async prune() {
        const cutoff = new Date(Date.now() - this.config.snapshotRetention);
        const { affected } = await this.snapshotRepo.delete({
            updatedAt: (0, typeorm_1.LessThan)(cutoff),
        });
        if (affected === 0) {
            this.logger.debug('Found no workflow snapshots to prune');
            return;
        }
        this.logger.debug('Pruned stale workflow snapshots', { count: affected });
    }
};
exports.SnapshotPruningService = SnapshotPruningService;
__decorate([
    (0, decorators_1.OnLeaderTakeover)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], SnapshotPruningService.prototype, "startPruning", null);
__decorate([
    (0, decorators_1.OnLeaderStepdown)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], SnapshotPruningService.prototype, "stopPruning", null);
__decorate([
    (0, decorators_1.OnShutdown)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], SnapshotPruningService.prototype, "shutdown", null);
exports.SnapshotPruningService = SnapshotPruningService = __decorate([
    (0, di_1.Service)(),
    __metadata("design:paramtypes", [backend_common_1.Logger,
        config_1.InstanceAiConfig,
        instance_ai_workflow_snapshot_repository_1.InstanceAiWorkflowSnapshotRepository,
        n8n_core_1.InstanceSettings])
], SnapshotPruningService);
//# sourceMappingURL=snapshot-pruning.service.js.map