import { __toESM } from "../../../../../../_virtual/rolldown_runtime.mjs";
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 { IsValidMessageType, parseMessageType, replaceLegacyDidSovPrefix } from "../../../util/messageType.mjs";
import { DidCommMessage } from "../../../DidCommMessage.mjs";
import { require_query_string } from "../../../../../../node_modules/.pnpm/query-string@7.1.3/node_modules/query-string/index.mjs";
import { CredoError, JsonEncoder, JsonTransformer } from "@credo-ts/core";
import { Transform } from "class-transformer";
import { ArrayNotEmpty, IsArray, IsOptional, IsString, IsUrl, ValidateIf } from "class-validator";

//#region ../didcomm/src/modules/connections/messages/DidCommConnectionInvitationMessage.ts
var import_query_string = /* @__PURE__ */ __toESM(require_query_string());
/**
* Message to invite another agent to create a connection
*
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0160-connection-protocol/README.md#0-invitation-to-connect
*/
var DidCommConnectionInvitationMessage = class DidCommConnectionInvitationMessage extends DidCommMessage {
	/**
	* Create new DidCommConnectionInvitationMessage instance.
	* @param options
	*/
	constructor(options) {
		super();
		this.allowDidSovPrefix = true;
		this.type = DidCommConnectionInvitationMessage.type.messageTypeUri;
		if (options) {
			this.id = options.id || this.generateId();
			this.label = options.label;
			this.imageUrl = options.imageUrl;
			this.appendedAttachments = options.appendedAttachments;
			if (isDidInvitation(options)) this.did = options.did;
			else {
				this.recipientKeys = options.recipientKeys;
				this.serviceEndpoint = options.serviceEndpoint;
				this.routingKeys = options.routingKeys;
			}
			if (options.did && (options.recipientKeys || options.routingKeys || options.serviceEndpoint)) throw new CredoError("either the did or the recipientKeys/serviceEndpoint/routingKeys must be set, but not both");
		}
	}
	/**
	* Create an invitation url from this instance
	*
	* @param domain domain name to use for invitation url
	* @returns invitation url with base64 encoded invitation
	*/
	toUrl({ domain, useDidSovPrefixWhereAllowed = false }) {
		const invitationJson = this.toJSON({ useDidSovPrefixWhereAllowed });
		return `${domain}?c_i=${JsonEncoder.toBase64URL(invitationJson)}`;
	}
	/**
	* Create a `DidCommConnectionInvitationMessage` instance from the `c_i` or `d_m` parameter of an URL
	*
	* @param invitationUrl invitation url containing c_i or d_m parameter
	*
	* @throws Error when the url can not be decoded to JSON, or decoded message is not a valid 'DidCommConnectionInvitationMessage'
	*/
	static fromUrl(invitationUrl) {
		const parsedUrl = (0, import_query_string.parseUrl)(invitationUrl).query;
		const encodedInvitation = parsedUrl.c_i ?? parsedUrl.d_m;
		if (typeof encodedInvitation === "string") {
			const invitationJson = JsonEncoder.fromBase64(encodedInvitation);
			return JsonTransformer.fromJSON(invitationJson, DidCommConnectionInvitationMessage);
		}
		throw new CredoError("InvitationUrl is invalid. Needs to be encoded with either c_i, d_m, or oob");
	}
};
DidCommConnectionInvitationMessage.type = parseMessageType("https://didcomm.org/connections/1.0/invitation");
__decorate([
	IsValidMessageType(DidCommConnectionInvitationMessage.type),
	Transform(({ value }) => replaceLegacyDidSovPrefix(value), { toClassOnly: true }),
	__decorateMetadata("design:type", Object)
], DidCommConnectionInvitationMessage.prototype, "type", void 0);
__decorate([IsString(), __decorateMetadata("design:type", String)], DidCommConnectionInvitationMessage.prototype, "label", void 0);
__decorate([
	IsString(),
	ValidateIf((o) => o.recipientKeys === void 0),
	__decorateMetadata("design:type", String)
], DidCommConnectionInvitationMessage.prototype, "did", void 0);
__decorate([
	IsString({ each: true }),
	IsArray(),
	ValidateIf((o) => o.did === void 0),
	ArrayNotEmpty(),
	__decorateMetadata("design:type", Array)
], DidCommConnectionInvitationMessage.prototype, "recipientKeys", void 0);
__decorate([
	IsString(),
	ValidateIf((o) => o.did === void 0),
	__decorateMetadata("design:type", String)
], DidCommConnectionInvitationMessage.prototype, "serviceEndpoint", void 0);
__decorate([
	IsString({ each: true }),
	ValidateIf((o) => o.did === void 0),
	IsOptional(),
	__decorateMetadata("design:type", Array)
], DidCommConnectionInvitationMessage.prototype, "routingKeys", void 0);
__decorate([
	IsOptional(),
	IsUrl(),
	__decorateMetadata("design:type", String)
], DidCommConnectionInvitationMessage.prototype, "imageUrl", void 0);
/**
* Check whether an invitation is a `DIDInvitationData` object
*
* @param invitation invitation object
*/
function isDidInvitation(invitation) {
	return invitation.did !== void 0;
}

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