import JSEncrypt from 'jsencrypt';
export async function getEncryptedData(data, base64PublicKey, requestProtocol = 'https') {
    if (requestProtocol === 'http') {
        const encrypt = new JSEncrypt();
        encrypt.setPublicKey(base64PublicKey);
        const enData = encrypt.encrypt(data);
        if (!enData) {
            throw new Error('JSEncrypt failed: Encryption returned false (check key or data length).');
        }
        return enData;
    }
    else {
        // Decode the base64-encoded public key
        const keyBytes = Uint8Array.from(atob(base64PublicKey), (c) => c.charCodeAt(0));
        // Import the public key in SPKI format
        const publicKey = await crypto.subtle.importKey('spki', keyBytes, {
            name: 'RSA-OAEP',
            hash: 'SHA-256',
        }, false, ['encrypt']);
        // Convert data to Uint8Array
        const encodedData = new TextEncoder().encode(data);
        // Encrypt the data using RSA-OAEP
        const encryptedData = await crypto.subtle.encrypt({
            name: 'RSA-OAEP',
        }, publicKey, encodedData);
        // Convert encrypted data to base64
        return btoa(String.fromCharCode(...new Uint8Array(encryptedData)));
    }
}
//# sourceMappingURL=getEncryptedData.js.map