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_DidCommProtocol = require('../../../../models/features/DidCommProtocol.js');
require('../../../../models/index.js');
const require_decorateParam = require('../../../../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateParam.js');
const require_DidCommFeatureRegistry = require('../../../../DidCommFeatureRegistry.js');
require('../../../../DidCommMessageHandlerRegistry.js');
const require_DidCommDiscoverFeaturesEvents = require('../../DidCommDiscoverFeaturesEvents.js');
const require_DidCommDiscoverFeaturesModuleConfig = require('../../DidCommDiscoverFeaturesModuleConfig.js');
const require_DidCommDiscoverFeaturesService = require('../../services/DidCommDiscoverFeaturesService.js');
const require_DidCommFeaturesDiscloseMessage = require('./messages/DidCommFeaturesDiscloseMessage.js');
const require_DidCommFeaturesQueryMessage = require('./messages/DidCommFeaturesQueryMessage.js');
require('./messages/index.js');
const require_DidCommFeaturesDiscloseMessageHandler = require('./handlers/DidCommFeaturesDiscloseMessageHandler.js');
const require_DidCommFeaturesQueryMessageHandler = require('./handlers/DidCommFeaturesQueryMessageHandler.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/discover-features/protocol/v1/DidCommDiscoverFeaturesV1Service.ts
var _ref, _ref2, _ref3;
let DidCommDiscoverFeaturesV1Service = class DidCommDiscoverFeaturesV1Service$1 extends require_DidCommDiscoverFeaturesService.DidCommDiscoverFeaturesService {
	constructor(featureRegistry, eventEmitter, logger, discoverFeaturesConfig) {
		super(featureRegistry, eventEmitter, logger, discoverFeaturesConfig);
		this.version = "v1";
	}
	register(messageHandlerRegistry) {
		messageHandlerRegistry.registerMessageHandler(new require_DidCommFeaturesDiscloseMessageHandler.DidCommFeaturesDiscloseMessageHandler(this));
		messageHandlerRegistry.registerMessageHandler(new require_DidCommFeaturesQueryMessageHandler.DidCommFeaturesQueryMessageHandler(this));
	}
	async createQuery(options) {
		if (options.queries.length > 1) throw new __credo_ts_core.CredoError("Discover Features V1 only supports a single query");
		if (options.queries[0].featureType !== "protocol") throw new __credo_ts_core.CredoError("Discover Features V1 only supports querying for protocol support");
		return { message: new require_DidCommFeaturesQueryMessage.DidCommFeaturesQueryMessage({
			query: options.queries[0].match,
			comment: options.comment
		}) };
	}
	async processQuery(messageContext) {
		const { query, threadId } = messageContext.message;
		const connection = messageContext.assertReadyConnection();
		this.eventEmitter.emit(messageContext.agentContext, {
			type: require_DidCommDiscoverFeaturesEvents.DidCommDiscoverFeaturesEventTypes.QueryReceived,
			payload: {
				message: messageContext.message,
				connection,
				queries: [{
					featureType: "protocol",
					match: query
				}],
				protocolVersion: this.version,
				threadId
			}
		});
		if (this.discoverFeaturesModuleConfig.autoAcceptQueries) return await this.createDisclosure({
			threadId,
			disclosureQueries: [{
				featureType: "protocol",
				match: query
			}]
		});
	}
	async createDisclosure(options) {
		if (options.disclosureQueries.some((item) => item.featureType !== "protocol")) throw new __credo_ts_core.CredoError("Discover Features V1 only supports protocols");
		if (!options.threadId) throw new __credo_ts_core.CredoError("Thread Id is required for Discover Features V1 disclosure");
		const matches = this.featureRegistry.query(...options.disclosureQueries);
		return { message: new require_DidCommFeaturesDiscloseMessage.DidCommFeaturesDiscloseMessage({
			threadId: options.threadId,
			protocols: matches.map((item) => new require_DidCommFeaturesDiscloseMessage.DidCommFeaturesDiscloseProtocol({
				protocolId: item.id,
				roles: item.roles
			}))
		}) };
	}
	async processDisclosure(messageContext) {
		const { protocols, threadId } = messageContext.message;
		const connection = messageContext.assertReadyConnection();
		this.eventEmitter.emit(messageContext.agentContext, {
			type: require_DidCommDiscoverFeaturesEvents.DidCommDiscoverFeaturesEventTypes.DisclosureReceived,
			payload: {
				message: messageContext.message,
				connection,
				disclosures: protocols.map((item) => new require_DidCommProtocol.DidCommProtocol({
					id: item.protocolId,
					roles: item.roles
				})),
				protocolVersion: this.version,
				threadId
			}
		});
	}
};
DidCommDiscoverFeaturesV1Service = require_decorate.__decorate([
	(0, __credo_ts_core.injectable)(),
	require_decorateParam.__decorateParam(2, (0, __credo_ts_core.inject)(__credo_ts_core.InjectionSymbols.Logger)),
	require_decorateMetadata.__decorateMetadata("design:paramtypes", [
		typeof (_ref = typeof require_DidCommFeatureRegistry.DidCommFeatureRegistry !== "undefined" && require_DidCommFeatureRegistry.DidCommFeatureRegistry) === "function" ? _ref : Object,
		typeof (_ref2 = typeof __credo_ts_core.EventEmitter !== "undefined" && __credo_ts_core.EventEmitter) === "function" ? _ref2 : Object,
		Object,
		typeof (_ref3 = typeof require_DidCommDiscoverFeaturesModuleConfig.DidCommDiscoverFeaturesModuleConfig !== "undefined" && require_DidCommDiscoverFeaturesModuleConfig.DidCommDiscoverFeaturesModuleConfig) === "function" ? _ref3 : Object
	])
], DidCommDiscoverFeaturesV1Service);

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