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 { DidCommBasicMessage } from "../messages/DidCommBasicMessage.mjs";
import "../messages/index.mjs";
import "../../connections/index.mjs";
import { DidCommBasicMessageEventTypes } from "../DidCommBasicMessageEvents.mjs";
import { DidCommBasicMessageRole } from "../DidCommBasicMessageRole.mjs";
import { DidCommBasicMessageRecord } from "../repository/DidCommBasicMessageRecord.mjs";
import { DidCommBasicMessageRepository } from "../repository/DidCommBasicMessageRepository.mjs";
import "../repository/index.mjs";
import { EventEmitter, injectable } from "@credo-ts/core";

//#region ../didcomm/src/modules/basic-messages/services/DidCommBasicMessageService.ts
var _ref, _ref2;
let DidCommBasicMessageService = class DidCommBasicMessageService$1 {
	constructor(basicMessageRepository, eventEmitter) {
		this.basicMessageRepository = basicMessageRepository;
		this.eventEmitter = eventEmitter;
	}
	async createMessage(agentContext, message, connectionRecord, parentThreadId) {
		const basicMessage = new DidCommBasicMessage({ content: message });
		if (parentThreadId) basicMessage.setThread({ parentThreadId });
		const basicMessageRecord = new DidCommBasicMessageRecord({
			sentTime: basicMessage.sentTime.toISOString(),
			content: basicMessage.content,
			connectionId: connectionRecord.id,
			role: DidCommBasicMessageRole.Sender,
			threadId: basicMessage.threadId,
			parentThreadId
		});
		await this.basicMessageRepository.save(agentContext, basicMessageRecord);
		this.emitStateChangedEvent(agentContext, basicMessageRecord, basicMessage);
		return {
			message: basicMessage,
			record: basicMessageRecord
		};
	}
	/**
	* @todo use connection from message context
	*/
	async save({ message, agentContext }, connection) {
		const basicMessageRecord = new DidCommBasicMessageRecord({
			sentTime: message.sentTime.toISOString(),
			content: message.content,
			connectionId: connection.id,
			role: DidCommBasicMessageRole.Receiver,
			threadId: message.threadId,
			parentThreadId: message.thread?.parentThreadId
		});
		await this.basicMessageRepository.save(agentContext, basicMessageRecord);
		this.emitStateChangedEvent(agentContext, basicMessageRecord, message);
	}
	emitStateChangedEvent(agentContext, basicMessageRecord, basicMessage) {
		this.eventEmitter.emit(agentContext, {
			type: DidCommBasicMessageEventTypes.DidCommBasicMessageStateChanged,
			payload: {
				message: basicMessage,
				basicMessageRecord: basicMessageRecord.clone()
			}
		});
	}
	async findAllByQuery(agentContext, query, queryOptions) {
		return this.basicMessageRepository.findByQuery(agentContext, query, queryOptions);
	}
	async getById(agentContext, basicMessageRecordId) {
		return this.basicMessageRepository.getById(agentContext, basicMessageRecordId);
	}
	async getByThreadId(agentContext, threadId) {
		return this.basicMessageRepository.getSingleByQuery(agentContext, { threadId });
	}
	async findAllByParentThreadId(agentContext, parentThreadId) {
		return this.basicMessageRepository.findByQuery(agentContext, { parentThreadId });
	}
	async deleteById(agentContext, basicMessageRecordId) {
		const basicMessageRecord = await this.getById(agentContext, basicMessageRecordId);
		return this.basicMessageRepository.delete(agentContext, basicMessageRecord);
	}
};
DidCommBasicMessageService = __decorate([injectable(), __decorateMetadata("design:paramtypes", [typeof (_ref = typeof DidCommBasicMessageRepository !== "undefined" && DidCommBasicMessageRepository) === "function" ? _ref : Object, typeof (_ref2 = typeof EventEmitter !== "undefined" && EventEmitter) === "function" ? _ref2 : Object])], DidCommBasicMessageService);

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