const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.js');
const require_decorateMetadata = require('../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateMetadata.js');
const require_decorate = require('../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorate.js');
const require_messageType = require('../util/messageType.js');
const require_decorateParam = require('../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateParam.js');
const require_DidCommMessageRecord = require('./DidCommMessageRecord.js');
let __credo_ts_core = require("@credo-ts/core");
__credo_ts_core = require_rolldown_runtime.__toESM(__credo_ts_core);

//#region ../didcomm/src/repository/DidCommMessageRepository.ts
var _ref;
let DidCommMessageRepository = class DidCommMessageRepository$1 extends __credo_ts_core.Repository {
	constructor(storageService, eventEmitter) {
		super(require_DidCommMessageRecord.DidCommMessageRecord, storageService, eventEmitter);
	}
	async saveAgentMessage(agentContext, { role, agentMessage, associatedRecordId }) {
		const didCommMessageRecord = new require_DidCommMessageRecord.DidCommMessageRecord({
			message: agentMessage.toJSON(),
			role,
			associatedRecordId
		});
		await this.save(agentContext, didCommMessageRecord);
	}
	async saveOrUpdateAgentMessage(agentContext, options) {
		const { messageName, protocolName, protocolMajorVersion } = require_messageType.parseMessageType(options.agentMessage.type);
		const record = await this.findSingleByQuery(agentContext, {
			associatedRecordId: options.associatedRecordId,
			messageName,
			protocolName,
			protocolMajorVersion: String(protocolMajorVersion)
		});
		if (record) {
			record.message = options.agentMessage.toJSON();
			record.role = options.role;
			await this.update(agentContext, record);
			return;
		}
		await this.saveAgentMessage(agentContext, options);
	}
	async getAgentMessage(agentContext, { associatedRecordId, messageClass, role }) {
		return (await this.getSingleByQuery(agentContext, {
			associatedRecordId,
			messageName: messageClass.type.messageName,
			protocolName: messageClass.type.protocolName,
			protocolMajorVersion: String(messageClass.type.protocolMajorVersion),
			role
		})).getMessageInstance(messageClass);
	}
	async findAgentMessage(agentContext, { associatedRecordId, messageClass, role }) {
		return (await this.findSingleByQuery(agentContext, {
			associatedRecordId,
			messageName: messageClass.type.messageName,
			protocolName: messageClass.type.protocolName,
			protocolMajorVersion: String(messageClass.type.protocolMajorVersion),
			role
		}))?.getMessageInstance(messageClass) ?? null;
	}
};
DidCommMessageRepository = require_decorate.__decorate([
	(0, __credo_ts_core.injectable)(),
	require_decorateParam.__decorateParam(0, (0, __credo_ts_core.inject)(__credo_ts_core.InjectionSymbols.StorageService)),
	require_decorateMetadata.__decorateMetadata("design:paramtypes", [Object, typeof (_ref = typeof __credo_ts_core.EventEmitter !== "undefined" && __credo_ts_core.EventEmitter) === "function" ? _ref : Object])
], DidCommMessageRepository);

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