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_index = require('../../../../../../node_modules/.pnpm/rxjs@7.8.2/node_modules/rxjs/dist/cjs/index.js');
const require_DidCommTransportEventTypes = require('../../../transport/DidCommTransportEventTypes.js');
require('../../../transport/index.js');
const require_DidCommMessagePickupEvents = require('../DidCommMessagePickupEvents.js');
let __credo_ts_core = require("@credo-ts/core");
__credo_ts_core = require_rolldown_runtime.__toESM(__credo_ts_core);

//#region ../didcomm/src/modules/message-pickup/services/DidCommMessagePickupSessionService.ts
var import_cjs = /* @__PURE__ */ require_rolldown_runtime.__toESM(require_index.require_cjs());
let DidCommMessagePickupSessionService = class DidCommMessagePickupSessionService$1 {
	constructor() {
		this.sessions = [];
	}
	start(agentContext) {
		const stop$ = agentContext.dependencyManager.resolve(__credo_ts_core.InjectionSymbols.Stop$);
		const eventEmitter = agentContext.dependencyManager.resolve(__credo_ts_core.EventEmitter);
		this.sessions = [];
		eventEmitter.observable(require_DidCommTransportEventTypes.DidCommTransportEventTypes.DidCommTransportSessionRemoved).pipe((0, import_cjs.takeUntil)(stop$)).subscribe({ next: (e) => {
			const liveModeSession = this.sessions.find((session) => session.transportSessionId === e.payload.session.id);
			if (liveModeSession) this.removeLiveSession(agentContext, { connectionId: liveModeSession.connectionId });
		} });
	}
	getLiveSession(_agentContext, sessionId) {
		return this.sessions.find((session) => session.id === sessionId);
	}
	getLiveSessionByConnectionId(_agentContext, options) {
		const { connectionId, role } = options;
		return this.sessions.find((session) => session.connectionId === connectionId && (role === void 0 || role === session.role));
	}
	saveLiveSession(agentContext, options) {
		const { connectionId, protocolVersion, role, transportSessionId } = options;
		this.removeLiveSession(agentContext, { connectionId });
		const session = {
			id: __credo_ts_core.utils.uuid(),
			connectionId,
			protocolVersion,
			role,
			transportSessionId
		};
		this.sessions.push(session);
		agentContext.dependencyManager.resolve(__credo_ts_core.EventEmitter).emit(agentContext, {
			type: require_DidCommMessagePickupEvents.DidCommMessagePickupEventTypes.LiveSessionSaved,
			payload: { session }
		});
	}
	removeLiveSession(agentContext, options) {
		const itemIndex = this.sessions.findIndex((session) => session.connectionId === options.connectionId);
		if (itemIndex > -1) {
			const [session] = this.sessions.splice(itemIndex, 1);
			agentContext.dependencyManager.resolve(__credo_ts_core.EventEmitter).emit(agentContext, {
				type: require_DidCommMessagePickupEvents.DidCommMessagePickupEventTypes.LiveSessionRemoved,
				payload: { session }
			});
		}
	}
};
DidCommMessagePickupSessionService = require_decorate.__decorate([(0, __credo_ts_core.injectable)(), require_decorateMetadata.__decorateMetadata("design:paramtypes", [])], DidCommMessagePickupSessionService);

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