import { __decorateMetadata } from "../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateMetadata.mjs";
import { __decorate } from "../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorate.mjs";
import { parseMessageType } from "../util/messageType.mjs";
import { __decorateParam } from "../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateParam.mjs";
import { DidCommMessageRecord } from "./DidCommMessageRecord.mjs";
import { EventEmitter, InjectionSymbols, Repository, inject, injectable } from "@credo-ts/core";

//#region ../didcomm/src/repository/DidCommMessageRepository.ts
var _ref;
let DidCommMessageRepository = class DidCommMessageRepository$1 extends Repository {
	constructor(storageService, eventEmitter) {
		super(DidCommMessageRecord, storageService, eventEmitter);
	}
	async saveAgentMessage(agentContext, { role, agentMessage, associatedRecordId }) {
		const didCommMessageRecord = new DidCommMessageRecord({
			message: agentMessage.toJSON(),
			role,
			associatedRecordId
		});
		await this.save(agentContext, didCommMessageRecord);
	}
	async saveOrUpdateAgentMessage(agentContext, options) {
		const { messageName, protocolName, protocolMajorVersion } = 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 = __decorate([
	injectable(),
	__decorateParam(0, inject(InjectionSymbols.StorageService)),
	__decorateMetadata("design:paramtypes", [Object, typeof (_ref = typeof EventEmitter !== "undefined" && EventEmitter) === "function" ? _ref : Object])
], DidCommMessageRepository);

//#endregion
export { DidCommMessageRepository };
//# sourceMappingURL=DidCommMessageRepository.mjs.map