/**
 * ConsoleEffectSink — logs effect emissions to a logger.
 *
 * Suppresses output during replay/inspect (when suppressExternal is true).
 *
 * @module ConsoleEffectSink
 */
import EffectSinkPort from "../../ports/EffectSinkPort.js";
import { createDeliveryObservation } from "../../domain/types/DeliveryObservation.js";
import { OUTCOME_DELIVERED, OUTCOME_SUPPRESSED, } from "../../domain/types/ExternalizationPolicy.js";
/** Default sink ID for ConsoleEffectSink. */
const CONSOLE_SINK_ID = 'console';
function buildSuppressedObservation(sinkId, emission, lens) {
    return createDeliveryObservation({
        emissionId: emission.id,
        sinkId,
        outcome: OUTCOME_SUPPRESSED,
        reason: `suppressed by ${lens.mode} lens`,
        timestamp: Date.now(),
        lens,
    });
}
function resolveSinkId(options) {
    if (options !== null && options !== undefined && typeof options.id === 'string' && options.id.length > 0) {
        return options.id;
    }
    return CONSOLE_SINK_ID;
}
function resolveLogger(options) {
    if (options !== null && options !== undefined && options.logger !== undefined) {
        return options.logger;
    }
    return null;
}
export class ConsoleEffectSink extends EffectSinkPort {
    _id;
    _logger;
    constructor(options) {
        super();
        this._id = resolveSinkId(options);
        this._logger = resolveLogger(options);
    }
    get id() {
        return this._id;
    }
    deliver(emission, lens) {
        if (lens.suppressExternal) {
            return Promise.resolve([buildSuppressedObservation(this._id, emission, lens)]);
        }
        if (this._logger !== null) {
            this._logger.info(`[effect:${emission.kind}]`, emission.id, emission.payload);
        }
        return Promise.resolve([createDeliveryObservation({
                emissionId: emission.id,
                sinkId: this._id,
                outcome: OUTCOME_DELIVERED,
                timestamp: Date.now(),
                lens,
            })]);
    }
}
