import { canHandleMessageType } from "../../util/messageType.mjs";
import { DidCommDidExchangeState } from "./models/DidCommDidExchangeState.mjs";
import { DidCommDidExchangeRole } from "./models/DidCommDidExchangeRole.mjs";
import "./models/index.mjs";
import { DidCommDidExchangeRequestMessage } from "./messages/DidCommDidExchangeRequestMessage.mjs";
import { DidCommDidExchangeResponseMessage } from "./messages/DidCommDidExchangeResponseMessage.mjs";
import { DidCommDidExchangeCompleteMessage } from "./messages/DidCommDidExchangeCompleteMessage.mjs";
import "./messages/index.mjs";
import { CredoError } from "@credo-ts/core";

//#region ../didcomm/src/modules/connections/DidExchangeStateMachine.ts
var DidExchangeStateMachine = class DidExchangeStateMachine {
	static assertCreateMessageState(messageType, record) {
		const rule = DidExchangeStateMachine.createMessageStateRules.find((r) => canHandleMessageType(r.message, messageType));
		if (!rule) throw new CredoError(`Could not find create message rule for ${messageType}`);
		if (rule.state !== record.state || rule.role !== record.role) throw new CredoError(`Record with role ${record.role} is in invalid state ${record.state} to create ${messageType}. Expected state for role ${rule.role} is ${rule.state}.`);
	}
	static assertProcessMessageState(messageType, record) {
		const rule = DidExchangeStateMachine.processMessageStateRules.find((r) => canHandleMessageType(r.message, messageType));
		if (!rule) throw new CredoError(`Could not find create message rule for ${messageType}`);
		if (rule.state !== record.state || rule.role !== record.role) throw new CredoError(`Record with role ${record.role} is in invalid state ${record.state} to process ${messageType.messageTypeUri}. Expected state for role ${rule.role} is ${rule.state}.`);
	}
	static nextState(messageType, record) {
		const rule = DidExchangeStateMachine.createMessageStateRules.concat(DidExchangeStateMachine.processMessageStateRules).find((r) => canHandleMessageType(r.message, messageType) && r.role === record.role);
		if (!rule) throw new CredoError(`Could not find create message rule for messageType ${messageType.messageTypeUri}, state ${record.state} and role ${record.role}`);
		return rule.nextState;
	}
};
DidExchangeStateMachine.createMessageStateRules = [
	{
		message: DidCommDidExchangeRequestMessage,
		state: DidCommDidExchangeState.InvitationReceived,
		role: DidCommDidExchangeRole.Requester,
		nextState: DidCommDidExchangeState.RequestSent
	},
	{
		message: DidCommDidExchangeResponseMessage,
		state: DidCommDidExchangeState.RequestReceived,
		role: DidCommDidExchangeRole.Responder,
		nextState: DidCommDidExchangeState.ResponseSent
	},
	{
		message: DidCommDidExchangeCompleteMessage,
		state: DidCommDidExchangeState.ResponseReceived,
		role: DidCommDidExchangeRole.Requester,
		nextState: DidCommDidExchangeState.Completed
	}
];
DidExchangeStateMachine.processMessageStateRules = [
	{
		message: DidCommDidExchangeRequestMessage,
		state: DidCommDidExchangeState.InvitationSent,
		role: DidCommDidExchangeRole.Responder,
		nextState: DidCommDidExchangeState.RequestReceived
	},
	{
		message: DidCommDidExchangeResponseMessage,
		state: DidCommDidExchangeState.RequestSent,
		role: DidCommDidExchangeRole.Requester,
		nextState: DidCommDidExchangeState.ResponseReceived
	},
	{
		message: DidCommDidExchangeCompleteMessage,
		state: DidCommDidExchangeState.ResponseSent,
		role: DidCommDidExchangeRole.Responder,
		nextState: DidCommDidExchangeState.Completed
	}
];

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