"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 SessionInputSubscriber_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionInputSubscriber = void 0;
const typeorm_1 = require("typeorm");
const common_1 = require("@nestjs/common");
const session_input_entity_1 = require("../core-entities/session-input.entity");
let SessionInputSubscriber = SessionInputSubscriber_1 = class SessionInputSubscriber {
    constructor() {
        this.logger = new common_1.Logger(SessionInputSubscriber_1.name);
    }
    listenTo() {
        return session_input_entity_1.SessionInput;
    }
    async beforeInsert(event) {
        const entity = event.entity;
        if (entity.sequence_number !== undefined &&
            entity.sequence_number !== null) {
            return;
        }
        const result = await event.manager
            .createQueryBuilder(session_input_entity_1.SessionInput, 'si')
            .select('MAX(si.sequence_number)', 'maxSequence')
            .where('si.session_id = :sessionId', { sessionId: entity.session_id })
            .getRawOne();
        entity.sequence_number = (result.maxSequence || 0) + 1;
    }
};
exports.SessionInputSubscriber = SessionInputSubscriber;
exports.SessionInputSubscriber = SessionInputSubscriber = SessionInputSubscriber_1 = __decorate([
    (0, typeorm_1.EventSubscriber)()
], SessionInputSubscriber);
//# sourceMappingURL=session-input.subscriber.js.map