

const require_rolldown_runtime = require('../../../../../_virtual/rolldown_runtime.js');
const require_KeyManagementError = require('../../../error/KeyManagementError.js');
const require_TypedArrayEncoder = require('../../../../../utils/TypedArrayEncoder.js');
require('../../../../../utils/index.js');
let ec_compression = require("ec-compression");

//#region src/modules/kms/jwk/kty/ec/ecPublicKey.ts
const ecCrvToCurveParams = {
	"P-256": ec_compression.Secp256r1,
	"P-384": ec_compression.Secp384r1,
	"P-521": ec_compression.Secp521r1,
	secp256k1: ec_compression.Secp256k1
};
function ecPublicJwkToPublicKey(publicJwk, { compressed = false } = {}) {
	const affinePoint = new ec_compression.AffinePoint(Uint8Array.from(require_TypedArrayEncoder.TypedArrayEncoder.fromBase64(publicJwk.x)), Uint8Array.from(require_TypedArrayEncoder.TypedArrayEncoder.fromBase64(publicJwk.y)));
	return compressed ? affinePoint.compressedForm : affinePoint.decompressedForm;
}
function ecPublicKeyToPublicJwk(publicKey, crv) {
	const curveParams = ecCrvToCurveParams[crv];
	if (!curveParams) throw new require_KeyManagementError.KeyManagementError(`kty EC with crv '${crv}' is not supported for creating jwk based on public key bytes`);
	let affinePoint;
	if ((0, ec_compression.isValidCompressedPublicKeyFormat)(publicKey, curveParams)) affinePoint = ec_compression.AffinePoint.fromCompressedPoint(publicKey, curveParams);
	else if ((0, ec_compression.isValidDecompressedPublicKeyFormat)(publicKey, curveParams)) affinePoint = ec_compression.AffinePoint.fromDecompressedPoint(publicKey, curveParams);
	else throw new require_KeyManagementError.KeyManagementError(`public key for kty EC with crv '${crv}' is neither a valid compressed or uncompressed key. Key prefix '${publicKey[0]}', key length '${publicKey.length}'`);
	return {
		kty: "EC",
		crv,
		x: require_TypedArrayEncoder.TypedArrayEncoder.toBase64URL(affinePoint.xBytes),
		y: require_TypedArrayEncoder.TypedArrayEncoder.toBase64URL(affinePoint.yBytes)
	};
}

//#endregion
exports.ecCrvToCurveParams = ecCrvToCurveParams;
exports.ecPublicJwkToPublicKey = ecPublicJwkToPublicKey;
exports.ecPublicKeyToPublicJwk = ecPublicKeyToPublicJwk;