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_DidCommBasicMessage = require('../messages/DidCommBasicMessage.js');
require('../messages/index.js');
require('../../connections/index.js');
const require_DidCommBasicMessageEvents = require('../DidCommBasicMessageEvents.js');
const require_DidCommBasicMessageRole = require('../DidCommBasicMessageRole.js');
const require_DidCommBasicMessageRecord = require('../repository/DidCommBasicMessageRecord.js');
const require_DidCommBasicMessageRepository = require('../repository/DidCommBasicMessageRepository.js');
require('../repository/index.js');
let __credo_ts_core = require("@credo-ts/core");
__credo_ts_core = require_rolldown_runtime.__toESM(__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 require_DidCommBasicMessage.DidCommBasicMessage({ content: message });
		if (parentThreadId) basicMessage.setThread({ parentThreadId });
		const basicMessageRecord = new require_DidCommBasicMessageRecord.DidCommBasicMessageRecord({
			sentTime: basicMessage.sentTime.toISOString(),
			content: basicMessage.content,
			connectionId: connectionRecord.id,
			role: require_DidCommBasicMessageRole.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 require_DidCommBasicMessageRecord.DidCommBasicMessageRecord({
			sentTime: message.sentTime.toISOString(),
			content: message.content,
			connectionId: connection.id,
			role: require_DidCommBasicMessageRole.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: require_DidCommBasicMessageEvents.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 = require_decorate.__decorate([(0, __credo_ts_core.injectable)(), require_decorateMetadata.__decorateMetadata("design:paramtypes", [typeof (_ref = typeof require_DidCommBasicMessageRepository.DidCommBasicMessageRepository !== "undefined" && require_DidCommBasicMessageRepository.DidCommBasicMessageRepository) === "function" ? _ref : Object, typeof (_ref2 = typeof __credo_ts_core.EventEmitter !== "undefined" && __credo_ts_core.EventEmitter) === "function" ? _ref2 : Object])], DidCommBasicMessageService);

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