const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.js');
const require_index = require('../../../../node_modules/.pnpm/class-validator@0.14.1/node_modules/class-validator/cjs/index.js');

//#region ../didcomm/src/util/messageType.ts
var import_cjs = /* @__PURE__ */ require_rolldown_runtime.__toESM(require_index.require_cjs());
const PROTOCOL_URI_REGEX = /^(.+)\/([^/\\]+)\/(\d+).(\d+)$/;
const MESSAGE_TYPE_REGEX = /^(.+)\/([^/\\]+)\/(\d+).(\d+)\/([^/\\]+)$/;
function parseMessageType(messageType) {
	const match = MESSAGE_TYPE_REGEX.exec(messageType);
	if (!match) throw new Error(`Invalid message type: ${messageType}`);
	const [, documentUri, protocolName, protocolVersionMajor, protocolVersionMinor, messageName] = match;
	return {
		documentUri,
		protocolName,
		protocolVersion: `${protocolVersionMajor}.${protocolVersionMinor}`,
		protocolMajorVersion: Number.parseInt(protocolVersionMajor),
		protocolMinorVersion: Number.parseInt(protocolVersionMinor),
		messageName,
		protocolUri: `${documentUri}/${protocolName}/${protocolVersionMajor}.${protocolVersionMinor}`,
		messageTypeUri: messageType
	};
}
function parseDidCommProtocolUri(didCommProtocolUri) {
	const match = PROTOCOL_URI_REGEX.exec(didCommProtocolUri);
	if (!match) throw new Error(`Invalid protocol uri: ${didCommProtocolUri}`);
	const [, documentUri, protocolName, protocolVersionMajor, protocolVersionMinor] = match;
	return {
		documentUri,
		protocolName,
		protocolVersion: `${protocolVersionMajor}.${protocolVersionMinor}`,
		protocolMajorVersion: Number.parseInt(protocolVersionMajor),
		protocolMinorVersion: Number.parseInt(protocolVersionMinor),
		protocolUri: `${documentUri}/${protocolName}/${protocolVersionMajor}.${protocolVersionMinor}`
	};
}
/**
* Check whether the incoming didcomm protocol uri is a protocol uri that can be handled by comparing it to the expected didcomm protocol uri.
* In this case the expected protocol uri is e.g. the handshake protocol supported (https://didcomm.org/connections/1.0), and the incoming protocol uri
* is the uri that is parsed from the incoming out of band invitation handshake_protocols.
*
* The method will make sure the following fields are equal:
*  - documentUri
*  - protocolName
*  - majorVersion
*
* If allowLegacyDidSovPrefixMismatch is true (default) it will allow for the case where the incoming protocol uri still has the legacy
* did:sov:BzCbsNYhMrjHiqZDTUASHg;spec did prefix, but the expected message type does not. This only works for incoming messages with a prefix
* of did:sov:BzCbsNYhMrjHiqZDTUASHg;spec and the expected message type having a prefix value of https:/didcomm.org
*
* @example
* const incomingProtocolUri = parseDidCommProtocolUri('https://didcomm.org/connections/1.0')
* const expectedProtocolUri = parseDidCommProtocolUri('https://didcomm.org/connections/1.4')
*
* // Returns true because the incoming protocol uri is equal to the expected protocol uri, except for
* // the minor version, which is lower
* const isIncomingProtocolUriSupported = supportsIncomingDidCommProtocolUri(incomingProtocolUri, expectedProtocolUri)
*
* @example
* const incomingProtocolUri = parseDidCommProtocolUri('did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0')
* const expectedProtocolUri = parseDidCommProtocolUri('https://didcomm.org/connections/1.0')
*
* // Returns true because the incoming protocol uri is equal to the expected protocol uri, except for
* // the legacy did sov prefix.
* const isIncomingProtocolUriSupported = supportsIncomingDidCommProtocolUri(incomingProtocolUri, expectedProtocolUri)
*/
function supportsIncomingDidCommProtocolUri(incomingProtocolUri, expectedProtocolUri, { allowLegacyDidSovPrefixMismatch = true } = {}) {
	const incomingDocumentUri = allowLegacyDidSovPrefixMismatch ? replaceLegacyDidSovPrefix(incomingProtocolUri.documentUri) : incomingProtocolUri.documentUri;
	const documentUriMatches = expectedProtocolUri.documentUri === incomingDocumentUri;
	const protocolNameMatches = expectedProtocolUri.protocolName === incomingProtocolUri.protocolName;
	const majorVersionMatches = expectedProtocolUri.protocolMajorVersion === incomingProtocolUri.protocolMajorVersion;
	return documentUriMatches && protocolNameMatches && majorVersionMatches;
}
/**
* Check whether the incoming message type is a message type that can be handled by comparing it to the expected message type.
* In this case the expected message type is e.g. the type declared on an agent message class, and the incoming message type is the type
* that is parsed from the incoming JSON.
*
* The method will make sure the following fields are equal:
*  - documentUri
*  - protocolName
*  - majorVersion
*  - messageName
*
* If allowLegacyDidSovPrefixMismatch is true (default) it will allow for the case where the incoming message type still has the legacy
* did:sov:BzCbsNYhMrjHiqZDTUASHg;spec did prefix, but the expected message type does not. This only works for incoming messages with a prefix
* of did:sov:BzCbsNYhMrjHiqZDTUASHg;spec and the expected message type having a prefix value of https:/didcomm.org
*
* @example
* const incomingMessageType = parseMessageType('https://didcomm.org/connections/1.0/request')
* const expectedMessageType = parseMessageType('https://didcomm.org/connections/1.4/request')
*
* // Returns true because the incoming message type is equal to the expected message type, except for
* // the minor version, which is lower
* const isIncomingMessageTypeSupported = supportsIncomingMessageType(incomingMessageType, expectedMessageType)
*
* @example
* const incomingMessageType = parseMessageType('did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/request')
* const expectedMessageType = parseMessageType('https://didcomm.org/connections/1.0/request')
*
* // Returns true because the incoming message type is equal to the expected message type, except for
* // the legacy did sov prefix.
* const isIncomingMessageTypeSupported = supportsIncomingMessageType(incomingMessageType, expectedMessageType)
*/
function supportsIncomingMessageType(incomingMessageType, expectedMessageType, { allowLegacyDidSovPrefixMismatch = true } = {}) {
	const incomingDocumentUri = allowLegacyDidSovPrefixMismatch ? replaceLegacyDidSovPrefix(incomingMessageType.documentUri) : incomingMessageType.documentUri;
	const documentUriMatches = expectedMessageType.documentUri === incomingDocumentUri;
	const protocolNameMatches = expectedMessageType.protocolName === incomingMessageType.protocolName;
	const majorVersionMatches = expectedMessageType.protocolMajorVersion === incomingMessageType.protocolMajorVersion;
	const messageNameMatches = expectedMessageType.messageName === incomingMessageType.messageName;
	return documentUriMatches && protocolNameMatches && majorVersionMatches && messageNameMatches;
}
function canHandleMessageType(messageClass, messageType) {
	return supportsIncomingMessageType(messageClass.type, messageType);
}
/**
* class-validator decorator to check if the string message type value matches with the
* expected message type. This uses {@link supportsIncomingMessageType}.
*/
function IsValidMessageType(messageType, validationOptions) {
	return (0, import_cjs.ValidateBy)({
		name: "isValidMessageType",
		constraints: [messageType],
		validator: {
			validate: (value, args) => {
				const [expectedMessageType] = args.constraints;
				if (typeof value !== "string") return false;
				return supportsIncomingMessageType(parseMessageType(value), expectedMessageType);
			},
			defaultMessage: (0, import_cjs.buildMessage)((eachPrefix) => `${eachPrefix}$property does not match the expected message type (only minor version may be lower)`, validationOptions)
		}
	}, validationOptions);
}
function replaceLegacyDidSovPrefixOnMessage(message) {
	message["@type"] = replaceLegacyDidSovPrefix(message["@type"]);
}
function replaceNewDidCommPrefixWithLegacyDidSovOnMessage(message) {
	message["@type"] = replaceNewDidCommPrefixWithLegacyDidSov(message["@type"]);
}
function replaceLegacyDidSovPrefix(messageType) {
	const didSovPrefix = "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec";
	const didCommPrefix = "https://didcomm.org";
	if (messageType.startsWith(didSovPrefix)) return messageType.replace(didSovPrefix, didCommPrefix);
	return messageType;
}
function replaceNewDidCommPrefixWithLegacyDidSov(messageType) {
	const didSovPrefix = "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec";
	const didCommPrefix = "https://didcomm.org";
	if (messageType.startsWith(didCommPrefix)) return messageType.replace(didCommPrefix, didSovPrefix);
	return messageType;
}

//#endregion
exports.IsValidMessageType = IsValidMessageType;
exports.canHandleMessageType = canHandleMessageType;
exports.parseDidCommProtocolUri = parseDidCommProtocolUri;
exports.parseMessageType = parseMessageType;
exports.replaceLegacyDidSovPrefix = replaceLegacyDidSovPrefix;
exports.replaceLegacyDidSovPrefixOnMessage = replaceLegacyDidSovPrefixOnMessage;
exports.replaceNewDidCommPrefixWithLegacyDidSovOnMessage = replaceNewDidCommPrefixWithLegacyDidSovOnMessage;
exports.supportsIncomingDidCommProtocolUri = supportsIncomingDidCommProtocolUri;
exports.supportsIncomingMessageType = supportsIncomingMessageType;