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 { DidCommAttachment, DidCommAttachmentData } from "../../../decorators/attachment/DidCommAttachment.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 { OutOfBandDidCommService } from "../domain/OutOfBandDidCommService.mjs";
import { outOfBandServiceToNumAlgo2Did } from "../helpers.mjs";
import { CredoError, IsStringOrInstance, JsonEncoder, JsonTransformer } from "@credo-ts/core";
import { Exclude, Expose, Transform, TransformationType, Type } from "class-transformer";
import { ArrayNotEmpty, IsArray, IsInstance, IsOptional, IsUrl, ValidateNested } from "class-validator";

//#region ../didcomm/src/modules/oob/messages/DidCommOutOfBandInvitation.ts
var import_query_string = /* @__PURE__ */ __toESM(require_query_string());
var _ref, _ref2;
/**
* The original invitation an out of band invitation was derived from.
*/
let DidCommInvitationType = /* @__PURE__ */ function(DidCommInvitationType$1) {
	DidCommInvitationType$1["OutOfBand"] = "out-of-band/1.x";
	DidCommInvitationType$1["Connection"] = "connections/1.x";
	DidCommInvitationType$1["Connectionless"] = "connectionless";
	return DidCommInvitationType$1;
}({});
var DidCommOutOfBandInvitation = class DidCommOutOfBandInvitation extends DidCommMessage {
	constructor(options) {
		super();
		this.type = DidCommOutOfBandInvitation.type.messageTypeUri;
		if (options) {
			this.id = options.id ?? this.generateId();
			this.label = options.label;
			this.goalCode = options.goalCode;
			this.goal = options.goal;
			this.accept = options.accept;
			this.handshakeProtocols = options.handshakeProtocols;
			this.services = options.services;
			this.imageUrl = options.imageUrl;
			this.appendedAttachments = options.appendedAttachments;
		}
	}
	addRequest(message) {
		if (!this.requests) this.requests = [];
		const requestAttachment = new DidCommAttachment({
			id: this.generateId(),
			mimeType: "application/json",
			data: new DidCommAttachmentData({ base64: JsonEncoder.toBase64(message.toJSON()) })
		});
		this.requests.push(requestAttachment);
	}
	getRequests() {
		return this.requests?.map((request) => request.getDataAsJson());
	}
	toUrl({ domain }) {
		const invitationJson = this.toJSON();
		return `${domain}?oob=${JsonEncoder.toBase64URL(invitationJson)}`;
	}
	static fromUrl(invitationUrl) {
		const encodedInvitation = (0, import_query_string.parseUrl)(invitationUrl).query.oob;
		if (typeof encodedInvitation === "string") {
			const invitationJson = JsonEncoder.fromBase64(encodedInvitation);
			return DidCommOutOfBandInvitation.fromJson(invitationJson);
		}
		throw new CredoError("InvitationUrl is invalid. It needs to contain one, and only one, of the following parameters; `oob`");
	}
	static fromJson(json) {
		return JsonTransformer.fromJSON(json, DidCommOutOfBandInvitation);
	}
	get invitationDids() {
		return this.getServices().map((didOrService) => {
			if (typeof didOrService === "string") return didOrService;
			return outOfBandServiceToNumAlgo2Did(didOrService);
		});
	}
	getServices() {
		return this.services.map((service) => {
			if (service instanceof String) return service.toString();
			return service;
		});
	}
	getDidServices() {
		return this.getServices().filter((service) => typeof service === "string");
	}
	getInlineServices() {
		return this.getServices().filter((service) => typeof service !== "string");
	}
};
DidCommOutOfBandInvitation.type = parseMessageType("https://didcomm.org/out-of-band/1.1/invitation");
__decorate([Exclude(), __decorateMetadata("design:type", typeof (_ref = typeof DidCommInvitationType !== "undefined" && DidCommInvitationType) === "function" ? _ref : Object)], DidCommOutOfBandInvitation.prototype, "invitationType", void 0);
__decorate([
	Transform(({ value }) => replaceLegacyDidSovPrefix(value), { toClassOnly: true }),
	IsValidMessageType(DidCommOutOfBandInvitation.type),
	__decorateMetadata("design:type", Object)
], DidCommOutOfBandInvitation.prototype, "type", void 0);
__decorate([Expose({ name: "goal_code" }), __decorateMetadata("design:type", String)], DidCommOutOfBandInvitation.prototype, "goalCode", void 0);
__decorate([
	Transform(({ value }) => value?.map(replaceLegacyDidSovPrefix), { toClassOnly: true }),
	Expose({ name: "handshake_protocols" }),
	__decorateMetadata("design:type", Array)
], DidCommOutOfBandInvitation.prototype, "handshakeProtocols", void 0);
__decorate([
	Expose({ name: "requests~attach" }),
	Type(() => DidCommAttachment),
	IsArray(),
	ValidateNested({ each: true }),
	IsInstance(DidCommAttachment, { each: true }),
	IsOptional(),
	__decorateMetadata("design:type", Array)
], DidCommOutOfBandInvitation.prototype, "requests", void 0);
__decorate([
	IsArray(),
	ArrayNotEmpty(),
	OutOfBandServiceTransformer(),
	IsStringOrInstance(OutOfBandDidCommService, { each: true }),
	__decorateMetadata("design:type", typeof (_ref2 = typeof Array !== "undefined" && Array) === "function" ? _ref2 : Object)
], DidCommOutOfBandInvitation.prototype, "services", void 0);
__decorate([
	IsOptional(),
	IsUrl(),
	__decorateMetadata("design:type", String)
], DidCommOutOfBandInvitation.prototype, "imageUrl", void 0);
/**
* Decorator that transforms services json to corresponding class instances
* @note Because of ValidateNested limitation, this produces instances of String for DID services except plain js string
*/
function OutOfBandServiceTransformer() {
	return Transform(({ value, type }) => {
		if (type === TransformationType.PLAIN_TO_CLASS) return value.map((service) => {
			if (typeof service === "string") return new String(service);
			return JsonTransformer.fromJSON(service, OutOfBandDidCommService);
		});
		if (type === TransformationType.CLASS_TO_PLAIN) return value.map((service) => typeof service === "string" || service instanceof String ? service.toString() : JsonTransformer.toJSON(service));
		return value;
	});
}

//#endregion
export { DidCommInvitationType, DidCommOutOfBandInvitation };
//# sourceMappingURL=DidCommOutOfBandInvitation.mjs.map