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 { takeUntil } from "../../../../../../node_modules/.pnpm/rxjs@7.8.2/node_modules/rxjs/dist/esm5/internal/operators/takeUntil.mjs";
import { DidCommTransportEventTypes } from "../../../transport/DidCommTransportEventTypes.mjs";
import "../../../transport/index.mjs";
import { DidCommMessagePickupEventTypes } from "../DidCommMessagePickupEvents.mjs";
import { EventEmitter, InjectionSymbols, injectable, utils } from "@credo-ts/core";

//#region ../didcomm/src/modules/message-pickup/services/DidCommMessagePickupSessionService.ts
let DidCommMessagePickupSessionService = class DidCommMessagePickupSessionService$1 {
	constructor() {
		this.sessions = [];
	}
	start(agentContext) {
		const stop$ = agentContext.dependencyManager.resolve(InjectionSymbols.Stop$);
		const eventEmitter = agentContext.dependencyManager.resolve(EventEmitter);
		this.sessions = [];
		eventEmitter.observable(DidCommTransportEventTypes.DidCommTransportSessionRemoved).pipe(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: utils.uuid(),
			connectionId,
			protocolVersion,
			role,
			transportSessionId
		};
		this.sessions.push(session);
		agentContext.dependencyManager.resolve(EventEmitter).emit(agentContext, {
			type: 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(EventEmitter).emit(agentContext, {
				type: DidCommMessagePickupEventTypes.LiveSessionRemoved,
				payload: { session }
			});
		}
	}
};
DidCommMessagePickupSessionService = __decorate([injectable(), __decorateMetadata("design:paramtypes", [])], DidCommMessagePickupSessionService);

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