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_DidCommOutboundMessageContext = require('../../models/DidCommOutboundMessageContext.js');
require('../../models/index.js');
const require_index$1 = require('../../../../../node_modules/.pnpm/rxjs@7.8.2/node_modules/rxjs/dist/cjs/index.js');
const require_decorateParam = require('../../../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateParam.js');
const require_index$2 = require('../../../../../node_modules/.pnpm/rxjs@7.8.2/node_modules/rxjs/dist/cjs/operators/index.js');
const require_DidCommConnectionService = require('../connections/services/DidCommConnectionService.js');
const require_DidCommMessageSender = require('../../DidCommMessageSender.js');
require('../connections/index.js');
const require_DidCommDiscoverFeaturesEvents = require('./DidCommDiscoverFeaturesEvents.js');
const require_DidCommDiscoverFeaturesModuleConfig = require('./DidCommDiscoverFeaturesModuleConfig.js');
const require_DidCommDiscoverFeaturesV1Service = require('./protocol/v1/DidCommDiscoverFeaturesV1Service.js');
const require_DidCommDiscoverFeaturesV2Service = require('./protocol/v2/DidCommDiscoverFeaturesV2Service.js');
require('./protocol/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/DidCommDiscoverFeaturesApi.ts
var import_cjs = /* @__PURE__ */ require_rolldown_runtime.__toESM(require_index$1.require_cjs());
var import_operators = /* @__PURE__ */ require_rolldown_runtime.__toESM(require_index$2.require_operators());
var _ref, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8;
let DidCommDiscoverFeaturesApi = class DidCommDiscoverFeaturesApi$1 {
	constructor(connectionService, messageSender, v1Service, v2Service, eventEmitter, stop$, agentContext, config) {
		this.connectionService = connectionService;
		this.messageSender = messageSender;
		this.eventEmitter = eventEmitter;
		this.stop$ = stop$;
		this.agentContext = agentContext;
		this.config = config;
		this.serviceMap = [v1Service, v2Service].reduce((serviceMap, service) => ({
			...serviceMap,
			[service.version]: service
		}), {});
	}
	getService(protocolVersion) {
		if (!this.serviceMap[protocolVersion]) throw new __credo_ts_core.CredoError(`No discover features service registered for protocol version ${protocolVersion}`);
		return this.serviceMap[protocolVersion];
	}
	/**
	* Send a query to an existing connection for discovering supported features of any kind. If desired, do the query synchronously,
	* meaning that it will await the response (or timeout) before resolving. Otherwise, response can be hooked by subscribing to
	* {DiscoverFeaturesDisclosureReceivedEvent}.
	*
	* Note: V1 protocol only supports a single query and is limited to protocols
	*
	* @param options feature queries to perform, protocol version and optional comment string (only used
	* in V1 protocol). If awaitDisclosures is set, perform the query synchronously with awaitDisclosuresTimeoutMs timeout.
	*/
	async queryFeatures(options) {
		const service = this.getService(options.protocolVersion);
		const connection = await this.connectionService.getById(this.agentContext, options.connectionId);
		const { message: queryMessage } = await service.createQuery({
			queries: options.queries,
			comment: options.comment
		});
		const outboundMessageContext = new require_DidCommOutboundMessageContext.DidCommOutboundMessageContext(queryMessage, {
			agentContext: this.agentContext,
			connection
		});
		const replaySubject = new import_cjs.ReplaySubject(1);
		if (options.awaitDisclosures) this.eventEmitter.observable(require_DidCommDiscoverFeaturesEvents.DidCommDiscoverFeaturesEventTypes.DisclosureReceived).pipe((0, import_operators.takeUntil)(this.stop$), (0, import_operators.filter)((e) => e.payload.connection?.id === connection.id), (0, import_operators.map)((e) => e.payload.disclosures), (0, import_operators.first)(), (0, import_operators.timeout)({
			first: options.awaitDisclosuresTimeoutMs ?? 7e3,
			meta: "DiscoverFeaturesApi.queryFeatures"
		}), (0, import_operators.catchError)(() => (0, import_cjs.of)([]))).subscribe(replaySubject);
		await this.messageSender.sendMessage(outboundMessageContext);
		return { features: options.awaitDisclosures ? await (0, import_cjs.firstValueFrom)(replaySubject) : void 0 };
	}
	/**
	* Disclose features to a connection, either proactively or as a response to a query.
	*
	* Features are disclosed based on queries that will be performed to Agent's Feature Registry,
	* meaning that they should be registered prior to disclosure. When sending disclosure as response,
	* these queries will usually match those from the corresponding Query or Queries message.
	*
	* Note: V1 protocol only supports sending disclosures as a response to a query.
	*
	* @param options multiple properties like protocol version to use, disclosure queries and thread id
	* (in case of disclosure as response to query)
	*/
	async discloseFeatures(options) {
		const service = this.getService(options.protocolVersion);
		const connection = await this.connectionService.getById(this.agentContext, options.connectionId);
		const { message: disclosuresMessage } = await service.createDisclosure({
			disclosureQueries: options.disclosureQueries,
			threadId: options.threadId
		});
		const outboundMessageContext = new require_DidCommOutboundMessageContext.DidCommOutboundMessageContext(disclosuresMessage, {
			agentContext: this.agentContext,
			connection
		});
		await this.messageSender.sendMessage(outboundMessageContext);
	}
};
DidCommDiscoverFeaturesApi = require_decorate.__decorate([
	(0, __credo_ts_core.injectable)(),
	require_decorateParam.__decorateParam(5, (0, __credo_ts_core.inject)(__credo_ts_core.InjectionSymbols.Stop$)),
	require_decorateMetadata.__decorateMetadata("design:paramtypes", [
		typeof (_ref = typeof require_DidCommConnectionService.DidCommConnectionService !== "undefined" && require_DidCommConnectionService.DidCommConnectionService) === "function" ? _ref : Object,
		typeof (_ref2 = typeof require_DidCommMessageSender.DidCommMessageSender !== "undefined" && require_DidCommMessageSender.DidCommMessageSender) === "function" ? _ref2 : Object,
		typeof (_ref3 = typeof require_DidCommDiscoverFeaturesV1Service.DidCommDiscoverFeaturesV1Service !== "undefined" && require_DidCommDiscoverFeaturesV1Service.DidCommDiscoverFeaturesV1Service) === "function" ? _ref3 : Object,
		typeof (_ref4 = typeof require_DidCommDiscoverFeaturesV2Service.DidCommDiscoverFeaturesV2Service !== "undefined" && require_DidCommDiscoverFeaturesV2Service.DidCommDiscoverFeaturesV2Service) === "function" ? _ref4 : Object,
		typeof (_ref5 = typeof __credo_ts_core.EventEmitter !== "undefined" && __credo_ts_core.EventEmitter) === "function" ? _ref5 : Object,
		typeof (_ref6 = typeof import_cjs.Subject !== "undefined" && import_cjs.Subject) === "function" ? _ref6 : Object,
		typeof (_ref7 = typeof __credo_ts_core.AgentContext !== "undefined" && __credo_ts_core.AgentContext) === "function" ? _ref7 : Object,
		typeof (_ref8 = typeof require_DidCommDiscoverFeaturesModuleConfig.DidCommDiscoverFeaturesModuleConfig !== "undefined" && require_DidCommDiscoverFeaturesModuleConfig.DidCommDiscoverFeaturesModuleConfig) === "function" ? _ref8 : Object
	])
], DidCommDiscoverFeaturesApi);

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