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 { DidCommProtocol } from "../../../../models/features/DidCommProtocol.mjs";
import "../../../../models/index.mjs";
import { __decorateParam } from "../../../../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateParam.mjs";
import { DidCommFeatureRegistry } from "../../../../DidCommFeatureRegistry.mjs";
import "../../../../DidCommMessageHandlerRegistry.mjs";
import { DidCommDiscoverFeaturesEventTypes } from "../../DidCommDiscoverFeaturesEvents.mjs";
import { DidCommDiscoverFeaturesModuleConfig } from "../../DidCommDiscoverFeaturesModuleConfig.mjs";
import { DidCommDiscoverFeaturesService } from "../../services/DidCommDiscoverFeaturesService.mjs";
import { DidCommFeaturesDiscloseMessage, DidCommFeaturesDiscloseProtocol } from "./messages/DidCommFeaturesDiscloseMessage.mjs";
import { DidCommFeaturesQueryMessage } from "./messages/DidCommFeaturesQueryMessage.mjs";
import "./messages/index.mjs";
import { DidCommFeaturesDiscloseMessageHandler } from "./handlers/DidCommFeaturesDiscloseMessageHandler.mjs";
import { DidCommFeaturesQueryMessageHandler } from "./handlers/DidCommFeaturesQueryMessageHandler.mjs";
import "./handlers/index.mjs";
import { CredoError, EventEmitter, InjectionSymbols, inject, injectable } from "@credo-ts/core";

//#region ../didcomm/src/modules/discover-features/protocol/v1/DidCommDiscoverFeaturesV1Service.ts
var _ref, _ref2, _ref3;
let DidCommDiscoverFeaturesV1Service = class DidCommDiscoverFeaturesV1Service$1 extends DidCommDiscoverFeaturesService {
	constructor(featureRegistry, eventEmitter, logger, discoverFeaturesConfig) {
		super(featureRegistry, eventEmitter, logger, discoverFeaturesConfig);
		this.version = "v1";
	}
	register(messageHandlerRegistry) {
		messageHandlerRegistry.registerMessageHandler(new DidCommFeaturesDiscloseMessageHandler(this));
		messageHandlerRegistry.registerMessageHandler(new DidCommFeaturesQueryMessageHandler(this));
	}
	async createQuery(options) {
		if (options.queries.length > 1) throw new CredoError("Discover Features V1 only supports a single query");
		if (options.queries[0].featureType !== "protocol") throw new CredoError("Discover Features V1 only supports querying for protocol support");
		return { message: new 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: 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 CredoError("Discover Features V1 only supports protocols");
		if (!options.threadId) throw new CredoError("Thread Id is required for Discover Features V1 disclosure");
		const matches = this.featureRegistry.query(...options.disclosureQueries);
		return { message: new DidCommFeaturesDiscloseMessage({
			threadId: options.threadId,
			protocols: matches.map((item) => new 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: DidCommDiscoverFeaturesEventTypes.DisclosureReceived,
			payload: {
				message: messageContext.message,
				connection,
				disclosures: protocols.map((item) => new DidCommProtocol({
					id: item.protocolId,
					roles: item.roles
				})),
				protocolVersion: this.version,
				threadId
			}
		});
	}
};
DidCommDiscoverFeaturesV1Service = __decorate([
	injectable(),
	__decorateParam(2, inject(InjectionSymbols.Logger)),
	__decorateMetadata("design:paramtypes", [
		typeof (_ref = typeof DidCommFeatureRegistry !== "undefined" && DidCommFeatureRegistry) === "function" ? _ref : Object,
		typeof (_ref2 = typeof EventEmitter !== "undefined" && EventEmitter) === "function" ? _ref2 : Object,
		Object,
		typeof (_ref3 = typeof DidCommDiscoverFeaturesModuleConfig !== "undefined" && DidCommDiscoverFeaturesModuleConfig) === "function" ? _ref3 : Object
	])
], DidCommDiscoverFeaturesV1Service);

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