"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.DomainEventPublisher = void 0;
const common_1 = require("@nestjs/common");
const outbox_notifier_1 = require("./outbox.notifier");
const outbox_service_1 = require("./outbox.service");
let DomainEventPublisher = class DomainEventPublisher {
    constructor(outboxService, outboxNotifier) {
        this.outboxService = outboxService;
        this.outboxNotifier = outboxNotifier;
    }
    async publishEvent(eventOrName, sourceModuleOrOptions, aggregateType, aggregateId, payload, metadata, options) {
        const payloadData = payload || {};
        const input = typeof eventOrName === 'string'
            ? {
                eventName: eventOrName,
                sourceModule: sourceModuleOrOptions,
                aggregateType: aggregateType,
                aggregateId: aggregateId,
                payload: payloadData,
                metadata,
            }
            : eventOrName;
        const publishOptions = typeof eventOrName === 'string'
            ? options
            : sourceModuleOrOptions;
        const persistedPayload = this.enrichPayloadWithMetadata(input.payload || {}, input.metadata);
        // Write to outbox (durable, authoritative)
        const outboxEvent = await this.outboxService.createEvent({
            eventName: input.eventName,
            sourceModule: input.sourceModule,
            aggregateType: input.aggregateType,
            aggregateId: input.aggregateId,
            payload: persistedPayload,
        }, publishOptions === null || publishOptions === void 0 ? void 0 : publishOptions.persistenceClient);
        // Notify processor for immediate wakeup (best-effort, not guaranteed)
        this.outboxNotifier.notifyEventWritten();
        return outboxEvent;
    }
    /**
     * Publish multiple events in sequence
     */
    async publishEvents(events, options) {
        const published = [];
        for (const event of events) {
            published.push(await this.publishEvent({
                eventName: event.eventName,
                sourceModule: event.sourceModule,
                aggregateType: event.aggregateType,
                aggregateId: event.aggregateId,
                payload: event.payload,
                metadata: event.metadata,
            }, options));
        }
        return published;
    }
    enrichPayloadWithMetadata(payload, metadata) {
        if (!metadata) {
            return payload;
        }
        return Object.assign(Object.assign({}, payload), { _integrationMeta: metadata });
    }
};
exports.DomainEventPublisher = DomainEventPublisher;
exports.DomainEventPublisher = DomainEventPublisher = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [outbox_service_1.OutboxService,
        outbox_notifier_1.OutboxNotifier])
], DomainEventPublisher);
//# sourceMappingURL=domain-event.publisher.js.map