const require_rolldown_runtime = require('../../../../../../../_virtual/rolldown_runtime.js');
const require_decorate = require('../../../../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorate.js');
const require_DidCommProtocol = require('../../../../models/features/DidCommProtocol.js');
const require_DidCommOutboundMessageContext = require('../../../../models/DidCommOutboundMessageContext.js');
require('../../../../models/index.js');
const require_DidCommEvents = require('../../../../DidCommEvents.js');
const require_DidCommModuleConfig = require('../../../../DidCommModuleConfig.js');
const require_DidCommMessagePickupEvents = require('../../DidCommMessagePickupEvents.js');
const require_DidCommMessagePickupModuleConfig = require('../../DidCommMessagePickupModuleConfig.js');
const require_DidCommBaseMessagePickupProtocol = require('../DidCommBaseMessagePickupProtocol.js');
const require_DidCommBatchMessage = require('./messages/DidCommBatchMessage.js');
const require_DidCommBatchPickupMessage = require('./messages/DidCommBatchPickupMessage.js');
require('./messages/index.js');
const require_DidCommBatchHandler = require('./handlers/DidCommBatchHandler.js');
const require_DidCommBatchPickupHandler = require('./handlers/DidCommBatchPickupHandler.js');
require('./handlers/index.js');
let __credo_ts_core = require("@credo-ts/core");
__credo_ts_core = require_rolldown_runtime.__toESM(__credo_ts_core);

//#region ../didcomm/src/modules/message-pickup/protocol/v1/DidCommMessagePickupV1Protocol.ts
let DidCommMessagePickupV1Protocol = class DidCommMessagePickupV1Protocol$1 extends require_DidCommBaseMessagePickupProtocol.DidCommBaseMessagePickupProtocol {
	constructor(..._args) {
		super(..._args);
		this.version = "v1";
	}
	/**
	* Registers the protocol implementation (handlers, feature registry) on the agent.
	*/
	register(messageHandlerRegistry, featureRegistry) {
		messageHandlerRegistry.registerMessageHandlers([new require_DidCommBatchPickupHandler.DidCommBatchPickupHandler(this), new require_DidCommBatchHandler.DidCommBatchHandler(this)]);
		featureRegistry.register(new require_DidCommProtocol.DidCommProtocol({
			id: "https://didcomm.org/messagepickup/1.0",
			roles: [
				"message_holder",
				"recipient",
				"batch_sender",
				"batch_recipient"
			]
		}));
	}
	async createPickupMessage(agentContext, options) {
		const { connectionRecord, batchSize } = options;
		connectionRecord.assertReady();
		const config = agentContext.dependencyManager.resolve(require_DidCommMessagePickupModuleConfig.DidCommMessagePickupModuleConfig);
		return { message: new require_DidCommBatchPickupMessage.DidCommBatchPickupMessage({ batchSize: batchSize ?? config.maximumBatchSize }) };
	}
	async createDeliveryMessage(agentContext, options) {
		const { connectionRecord, batchSize, messages } = options;
		connectionRecord.assertReady();
		const queueTransportRepository = agentContext.dependencyManager.resolve(require_DidCommModuleConfig.DidCommModuleConfig).queueTransportRepository;
		const messagesToDeliver = messages ?? await queueTransportRepository.takeFromQueue(agentContext, {
			connectionId: connectionRecord.id,
			limit: batchSize,
			deleteMessages: true
		});
		const batchMessages = messagesToDeliver.map((msg) => new require_DidCommBatchMessage.DidCommBatchMessageMessage({
			id: msg.id,
			message: msg.encryptedMessage
		}));
		if (messagesToDeliver.length > 0) return { message: new require_DidCommBatchMessage.DidCommBatchMessage({ messages: batchMessages }) };
	}
	async setLiveDeliveryMode() {
		throw new __credo_ts_core.CredoError("Live Delivery mode not supported in Message Pickup V1 protocol");
	}
	async processBatchPickup(messageContext) {
		const connection = messageContext.assertReadyConnection();
		const { message, agentContext } = messageContext;
		return new require_DidCommOutboundMessageContext.DidCommOutboundMessageContext(new require_DidCommBatchMessage.DidCommBatchMessage({
			messages: (await agentContext.dependencyManager.resolve(require_DidCommModuleConfig.DidCommModuleConfig).queueTransportRepository.takeFromQueue(agentContext, {
				connectionId: connection.id,
				limit: message.batchSize,
				deleteMessages: true
			})).map((msg) => new require_DidCommBatchMessage.DidCommBatchMessageMessage({
				id: msg.id,
				message: msg.encryptedMessage
			})),
			threadId: message.threadId
		}), {
			agentContext: messageContext.agentContext,
			connection
		});
	}
	async processBatch(messageContext) {
		const { message: batchMessage, agentContext } = messageContext;
		const { messages } = batchMessage;
		const connection = messageContext.assertReadyConnection();
		const eventEmitter = messageContext.agentContext.dependencyManager.resolve(__credo_ts_core.EventEmitter);
		for (const message of messages) eventEmitter.emit(messageContext.agentContext, {
			type: require_DidCommEvents.DidCommEventTypes.DidCommMessageReceived,
			payload: {
				message: message.message,
				contextCorrelationId: messageContext.agentContext.contextCorrelationId
			}
		});
		if (messages.length === 0) {
			eventEmitter.emit(messageContext.agentContext, {
				type: require_DidCommMessagePickupEvents.DidCommMessagePickupEventTypes.MessagePickupCompleted,
				payload: {
					connection,
					threadId: batchMessage.threadId
				}
			});
			return null;
		}
		return (await this.createPickupMessage(agentContext, { connectionRecord: connection })).message;
	}
};
DidCommMessagePickupV1Protocol = require_decorate.__decorate([(0, __credo_ts_core.injectable)()], DidCommMessagePickupV1Protocol);

//#endregion
Object.defineProperty(exports, 'DidCommMessagePickupV1Protocol', {
  enumerable: true,
  get: function () {
    return DidCommMessagePickupV1Protocol;
  }
});