

const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.js');
const require_CredoError = require('../../../error/CredoError.js');
require('../../../error/index.js');
const require_decorateMetadata = require('../../../_virtual/_@oxc-project_runtime@0.97.0/helpers/decorateMetadata.js');
const require_decorate = require('../../../_virtual/_@oxc-project_runtime@0.97.0/helpers/decorate.js');
const require_TypedArrayEncoder = require('../../../utils/TypedArrayEncoder.js');
const require_JsonTransformer = require('../../../utils/JsonTransformer.js');
const require_transformers = require('../../../utils/transformers.js');
require('../../../utils/index.js');
const require_X25519PublicJwk = require('../../kms/jwk/kty/okp/X25519PublicJwk.js');
const require_Ed25519PublicJwk = require('../../kms/jwk/kty/okp/Ed25519PublicJwk.js');
const require_PublicJwk = require('../../kms/jwk/PublicJwk.js');
require('../../kms/index.js');
const require_VerificationMethod = require('./verificationMethod/VerificationMethod.js');
const require_VerificationMethodTransformer = require('./verificationMethod/VerificationMethodTransformer.js');
require('./verificationMethod/index.js');
const require_keyDidMapping = require('./key-type/keyDidMapping.js');
const require_findMatchingEd25519Key = require('../findMatchingEd25519Key.js');
require('./key-type/index.js');
const require_DidCommV1Service = require('./service/DidCommV1Service.js');
const require_IndyAgentService = require('./service/IndyAgentService.js');
const require_ServiceTransformer = require('./service/ServiceTransformer.js');
require('./service/index.js');
let class_transformer = require("class-transformer");
let class_validator = require("class-validator");

//#region src/modules/dids/domain/DidDocument.ts
var _ref, _ref2, _ref3, _ref4, _ref5;
var DidDocument = class DidDocument {
	constructor(options) {
		this.context = ["https://www.w3.org/ns/did/v1"];
		if (options) {
			this.context = options.context ?? this.context;
			this.id = options.id;
			this.alsoKnownAs = options.alsoKnownAs;
			this.controller = options.controller;
			this.verificationMethod = options.verificationMethod;
			this.service = options.service;
			this.authentication = options.authentication;
			this.assertionMethod = options.assertionMethod;
			this.keyAgreement = options.keyAgreement;
			this.capabilityInvocation = options.capabilityInvocation;
			this.capabilityDelegation = options.capabilityDelegation;
		}
	}
	dereferenceVerificationMethod(keyId) {
		const verificationMethod = this.verificationMethod?.find((key) => key.id.endsWith(keyId));
		if (!verificationMethod) throw new require_CredoError.CredoError(`Unable to locate verification method with id '${keyId}'`);
		return verificationMethod;
	}
	dereferenceKey(keyId, allowedPurposes) {
		const purposes = allowedPurposes ?? [
			"authentication",
			"keyAgreement",
			"assertionMethod",
			"capabilityInvocation",
			"capabilityDelegation",
			"verificationMethod"
		];
		for (const purpose of purposes) for (const key of this[purpose] ?? []) {
			if (typeof key === "string" && key.endsWith(keyId)) return this.dereferenceVerificationMethod(key);
			if (typeof key !== "string" && key.id.endsWith(keyId)) return key;
		}
		throw new require_CredoError.CredoError(`Unable to locate verification method with id '${keyId}' in purposes ${purposes}`);
	}
	findVerificationMethodByPublicKey(publicJwk, allowedPurposes) {
		const purposes = allowedPurposes ?? [
			"authentication",
			"keyAgreement",
			"assertionMethod",
			"capabilityInvocation",
			"capabilityDelegation",
			"verificationMethod"
		];
		for (const purpose of purposes) for (const key of this[purpose] ?? []) {
			const verificationMethod = typeof key === "string" ? this.dereferenceVerificationMethod(key) : key;
			if (require_keyDidMapping.getPublicJwkFromVerificationMethod(verificationMethod).equals(publicJwk)) return verificationMethod;
		}
		throw new require_CredoError.CredoError(`Unable to locate verification method with public key ${publicJwk.jwkTypehumanDescription} in purposes ${purposes}`);
	}
	/**
	* Returns all of the service endpoints matching the given type.
	*
	* @param type The type of service(s) to query.
	*/
	getServicesByType(type) {
		return this.service?.filter((service) => service.type === type) ?? [];
	}
	/**
	* Returns all of the service endpoints matching the given class
	*
	* @param classType The class to query services.
	*/
	getServicesByClassType(classType) {
		return this.service?.filter((service) => service instanceof classType) ?? [];
	}
	/**
	* Get all DIDComm services ordered by priority descending. This means the highest
	* priority will be the first entry.
	*/
	get didCommServices() {
		const didCommServiceTypes = [require_IndyAgentService.IndyAgentService.type, require_DidCommV1Service.DidCommV1Service.type];
		return (this.service?.filter((service) => didCommServiceTypes.includes(service.type)) ?? []).sort((a, b) => a.priority - b.priority);
	}
	get recipientKeys() {
		return this.getRecipientKeysWithVerificationMethod({ mapX25519ToEd25519: false }).map(({ publicJwk }) => publicJwk);
	}
	/**
	* Returns the recipient keys with their verification method matches
	*
	* We should probably deprecate recipientKeys in favour of this one
	*/
	getRecipientKeysWithVerificationMethod({ mapX25519ToEd25519 }) {
		const recipientKeys = [];
		const seenVerificationMethodIds = [];
		for (const service of this.didCommServices) if (service.type === require_IndyAgentService.IndyAgentService.type) for (const publicKeyBase58 of service.recipientKeys) {
			const publicJwk = require_PublicJwk.PublicJwk.fromPublicKey({
				kty: "OKP",
				crv: "Ed25519",
				publicKey: require_TypedArrayEncoder.TypedArrayEncoder.fromBase58(publicKeyBase58)
			});
			const verificationMethod = [...this.verificationMethod ?? [], ...this.authentication ?? []].map((v) => typeof v === "string" ? this.dereferenceVerificationMethod(v) : v).find((v) => {
				return require_keyDidMapping.getPublicJwkFromVerificationMethod(v).equals(publicJwk);
			});
			if (!verificationMethod) throw new require_CredoError.CredoError("Could not find verification method for IndyAgentService recipient key");
			if (seenVerificationMethodIds.includes(verificationMethod.id)) continue;
			recipientKeys.push({
				publicJwk,
				verificationMethod
			});
		}
		else if (service.type === require_DidCommV1Service.DidCommV1Service.type) for (const recipientKey of service.recipientKeys) {
			const verificationMethod = this.dereferenceKey(recipientKey, ["authentication", "keyAgreement"]);
			if (seenVerificationMethodIds.includes(verificationMethod.id)) continue;
			const publicJwk = require_keyDidMapping.getPublicJwkFromVerificationMethod(verificationMethod);
			if (!publicJwk.is(require_Ed25519PublicJwk.Ed25519PublicJwk, require_X25519PublicJwk.X25519PublicJwk)) throw new require_CredoError.CredoError("Expected either Ed25519PublicJwk or X25519PublicJwk for DidcommV1Service recipient key");
			recipientKeys.push({
				publicJwk,
				verificationMethod
			});
		}
		if (!mapX25519ToEd25519) return recipientKeys;
		return recipientKeys.map(({ publicJwk, verificationMethod }) => {
			if (publicJwk.is(require_Ed25519PublicJwk.Ed25519PublicJwk)) return {
				publicJwk,
				verificationMethod
			};
			const matchingEd25519Key = require_findMatchingEd25519Key.findMatchingEd25519Key(publicJwk, this);
			if (!matchingEd25519Key) throw new require_CredoError.CredoError(`Unable to find matching Ed25519 key for X25519 verification method with id ${verificationMethod.id}`);
			return matchingEd25519Key;
		});
	}
	toJSON() {
		return require_JsonTransformer.JsonTransformer.toJSON(this);
	}
	static fromJSON(didDocument) {
		return require_JsonTransformer.JsonTransformer.fromJSON(didDocument, DidDocument);
	}
};
require_decorate.__decorate([
	(0, class_transformer.Expose)({ name: "@context" }),
	require_transformers.IsStringOrStringArray(),
	require_decorateMetadata.__decorateMetadata("design:type", Object)
], DidDocument.prototype, "context", void 0);
require_decorate.__decorate([(0, class_validator.IsString)(), require_decorateMetadata.__decorateMetadata("design:type", String)], DidDocument.prototype, "id", void 0);
require_decorate.__decorate([
	(0, class_validator.IsArray)(),
	(0, class_validator.IsString)({ each: true }),
	(0, class_validator.IsOptional)(),
	require_decorateMetadata.__decorateMetadata("design:type", Array)
], DidDocument.prototype, "alsoKnownAs", void 0);
require_decorate.__decorate([
	require_transformers.IsStringOrStringArray(),
	(0, class_validator.IsOptional)(),
	require_decorateMetadata.__decorateMetadata("design:type", Object)
], DidDocument.prototype, "controller", void 0);
require_decorate.__decorate([
	(0, class_validator.IsArray)(),
	(0, class_validator.ValidateNested)({ each: true }),
	(0, class_transformer.Type)(() => require_VerificationMethod.VerificationMethod),
	(0, class_validator.IsOptional)(),
	require_decorateMetadata.__decorateMetadata("design:type", Array)
], DidDocument.prototype, "verificationMethod", void 0);
require_decorate.__decorate([
	(0, class_validator.IsArray)(),
	require_ServiceTransformer.ServiceTransformer(),
	(0, class_validator.IsOptional)(),
	require_decorateMetadata.__decorateMetadata("design:type", Array)
], DidDocument.prototype, "service", void 0);
require_decorate.__decorate([
	(0, class_validator.IsArray)(),
	require_VerificationMethodTransformer.VerificationMethodTransformer(),
	require_VerificationMethodTransformer.IsStringOrVerificationMethod({ each: true }),
	(0, class_validator.IsOptional)(),
	require_decorateMetadata.__decorateMetadata("design:type", typeof (_ref = typeof Array !== "undefined" && Array) === "function" ? _ref : Object)
], DidDocument.prototype, "authentication", void 0);
require_decorate.__decorate([
	(0, class_validator.IsArray)(),
	require_VerificationMethodTransformer.VerificationMethodTransformer(),
	require_VerificationMethodTransformer.IsStringOrVerificationMethod({ each: true }),
	(0, class_validator.IsOptional)(),
	require_decorateMetadata.__decorateMetadata("design:type", typeof (_ref2 = typeof Array !== "undefined" && Array) === "function" ? _ref2 : Object)
], DidDocument.prototype, "assertionMethod", void 0);
require_decorate.__decorate([
	(0, class_validator.IsArray)(),
	require_VerificationMethodTransformer.VerificationMethodTransformer(),
	require_VerificationMethodTransformer.IsStringOrVerificationMethod({ each: true }),
	(0, class_validator.IsOptional)(),
	require_decorateMetadata.__decorateMetadata("design:type", typeof (_ref3 = typeof Array !== "undefined" && Array) === "function" ? _ref3 : Object)
], DidDocument.prototype, "keyAgreement", void 0);
require_decorate.__decorate([
	(0, class_validator.IsArray)(),
	require_VerificationMethodTransformer.VerificationMethodTransformer(),
	require_VerificationMethodTransformer.IsStringOrVerificationMethod({ each: true }),
	(0, class_validator.IsOptional)(),
	require_decorateMetadata.__decorateMetadata("design:type", typeof (_ref4 = typeof Array !== "undefined" && Array) === "function" ? _ref4 : Object)
], DidDocument.prototype, "capabilityInvocation", void 0);
require_decorate.__decorate([
	(0, class_validator.IsArray)(),
	require_VerificationMethodTransformer.VerificationMethodTransformer(),
	require_VerificationMethodTransformer.IsStringOrVerificationMethod({ each: true }),
	(0, class_validator.IsOptional)(),
	require_decorateMetadata.__decorateMetadata("design:type", typeof (_ref5 = typeof Array !== "undefined" && Array) === "function" ? _ref5 : Object)
], DidDocument.prototype, "capabilityDelegation", void 0);
/**
* Extracting the verification method for signature type
* @param type Signature type
* @param didDocument DidDocument
* @returns verification method
*/
async function findVerificationMethodByKeyType(keyType, didDocument) {
	for (const purpose of [
		"verificationMethod",
		"authentication",
		"keyAgreement",
		"assertionMethod",
		"capabilityInvocation",
		"capabilityDelegation"
	]) {
		const key = didDocument[purpose];
		if (Array.isArray(key)) {
			for (const method of key) if (typeof method !== "string") {
				if (method.type === keyType) return method;
			}
		}
	}
	return null;
}

//#endregion
exports.DidDocument = DidDocument;
exports.findVerificationMethodByKeyType = findVerificationMethodByKeyType;