import { PublicKey } from "../publicKey/PublicKey.mjs";
import { publicKeyTypes } from "../publicKey/index.mjs";
import { Authentication } from "./Authentication.mjs";
import { EmbeddedAuthentication } from "./EmbeddedAuthentication.mjs";
import { ReferencedAuthentication } from "./ReferencedAuthentication.mjs";
import { CredoError } from "@credo-ts/core";
import { Transform, TransformationType, instanceToPlain, plainToInstance } from "class-transformer";

//#region ../didcomm/src/modules/connections/models/did/authentication/index.ts
const authenticationTypes = {
	RsaVerificationKey2018: "RsaSignatureAuthentication2018",
	Ed25519VerificationKey2018: "Ed25519SignatureAuthentication2018",
	Secp256k1VerificationKey2018: "Secp256k1SignatureAuthenticationKey2018"
};
/**
* Decorator that transforms authentication json to corresponding class instances. See {@link authenticationTypes}
*
* @example
* class Example {
*   AuthenticationTransformer()
*   private authentication: Authentication
* }
*/
function AuthenticationTransformer() {
	return Transform(({ value, obj, type }) => {
		if (type === TransformationType.PLAIN_TO_CLASS) return value.map((auth) => {
			if (auth.publicKey) {
				const publicKeyJson = obj.publicKey.find((publicKey) => publicKey.id === auth.publicKey);
				if (!publicKeyJson) throw new CredoError(`Invalid public key referenced ${auth.publicKey}`);
				return new ReferencedAuthentication(plainToInstance(publicKeyTypes[publicKeyJson.type] ?? PublicKey, publicKeyJson), auth.type);
			}
			return new EmbeddedAuthentication(plainToInstance(publicKeyTypes[auth.type] ?? PublicKey, auth));
		});
		return value.map((auth) => auth instanceof EmbeddedAuthentication ? instanceToPlain(auth.publicKey) : auth);
	});
}

//#endregion
export { AuthenticationTransformer, authenticationTypes };
//# sourceMappingURL=index.mjs.map