

import { P256PublicJwk } from "../../modules/kms/jwk/kty/ec/P256PublicJwk.mjs";
import { P384PublicJwk } from "../../modules/kms/jwk/kty/ec/P384PublicJwk.mjs";
import { P521PublicJwk } from "../../modules/kms/jwk/kty/ec/P521PublicJwk.mjs";
import { Secp256k1PublicJwk } from "../../modules/kms/jwk/kty/ec/Secp256k1PublicJwk.mjs";
import { Ed25519PublicJwk } from "../../modules/kms/jwk/kty/okp/Ed25519PublicJwk.mjs";
import { RsaPublicJwk } from "../../modules/kms/jwk/kty/rsa/RsaPublicJwk.mjs";
import "../../modules/kms/index.mjs";
import { CredoWebCryptoError } from "./CredoWebCryptoError.mjs";

//#region src/crypto/webcrypto/types.ts
/**
* Derives the JWA algorithm name from KeySignParams or KeyVerifyParams
* @param params - The signing or verification parameters
* @returns The corresponding JWA algorithm string
*/
function keyParamsToJwaAlgorithm(params, key) {
	if (params.name === "Ed25519") {
		if (!key.publicJwk.is(Ed25519PublicJwk)) throw new CredoWebCryptoError(`Unsupported key for algorithm for Ed25519: ${key.publicJwk.jwkTypehumanDescription}`);
		return "EdDSA";
	}
	if (params.name === "ECDSA") {
		const hashName = typeof params.hash === "string" ? params.hash : params.hash.name;
		if (key.publicJwk.is(Secp256k1PublicJwk)) switch (hashName) {
			case "SHA-256": return "ES256K";
			default: throw new CredoWebCryptoError(`Unsupported hash algorithm for ECDSA with Secp255K1: ${hashName}`);
		}
		if (key.publicJwk.is(P256PublicJwk)) switch (hashName) {
			case "SHA-256": return "ES256";
			default: throw new CredoWebCryptoError(`Unsupported hash algorithm for ECDSA with P256: ${hashName}`);
		}
		if (key.publicJwk.is(P384PublicJwk)) switch (hashName) {
			case "SHA-384": return "ES384";
			default: throw new CredoWebCryptoError(`Unsupported hash algorithm for ECDSA with P384: ${hashName}`);
		}
		if (key.publicJwk.is(P521PublicJwk)) switch (hashName) {
			case "SHA-512": return "ES512";
			default: throw new CredoWebCryptoError(`Unsupported hash algorithm for ECDSA with P521: ${hashName}`);
		}
		throw new CredoWebCryptoError(`Unsupported key ${key.publicJwk.jwkTypehumanDescription} or hash algorithm '${hashName}' for ECDSA`);
	}
	if (params.name === "RSASSA-PKCS1-v1_5") {
		const hashName = typeof params.hash === "string" ? params.hash : params.hash.name;
		if (!key.publicJwk.is(RsaPublicJwk)) throw new CredoWebCryptoError(`Unsupported key for algorithm for RSASSA-PKCS1-v1_5: ${key.publicJwk.jwkTypehumanDescription}`);
		switch (hashName) {
			case "SHA-256": return "RS256";
			case "SHA-384": return "RS384";
			case "SHA-512": return "RS512";
			default: throw new CredoWebCryptoError(`Unsupported hash algorithm for RSASSA-PKCS1-v1_5: ${hashName}`);
		}
	}
	if (params.name === "RSA-PSS") {
		const hashName = typeof params.hash === "string" ? params.hash : params.hash.name;
		if (!key.publicJwk.is(RsaPublicJwk)) throw new CredoWebCryptoError(`Unsupported key for algorithm for RSA-PSS: ${key.publicJwk.jwkTypehumanDescription}`);
		switch (hashName) {
			case "SHA-256": return "PS256";
			case "SHA-384": return "PS384";
			case "SHA-512": return "PS512";
			default: throw new CredoWebCryptoError(`Unsupported hash algorithm for RSA-PSS: ${hashName}`);
		}
	}
	throw new CredoWebCryptoError(`Unsupported algorithm: ${params.name}`);
}

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