import { logger } from '../../core/src/utils/logger.mjs';

const PKCE_VERIFIER_STORAGE_KEY = 'PKCE_VERIFIER';
function toHex(n) {
    let str = n.toString(16);
    if (str.length === 1) {
        str = '0' + str;
    }
    return str;
}
function base64URLEncode(buf) {
    // Convert the ArrayBuffer to string using Uint8 array.
    // btoa takes chars from 0-255 and base64 encodes.
    // Then convert the base64 encoded to base64url encoded.
    // (replace + with -, replace / with _, trim trailing =)
    return btoa(String.fromCharCode.call(null, ...new Uint8Array(buf))).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
class PKCEManager {
    _dataLayer;
    namespace;
    constructor(_dataLayer, namespace){
        this._dataLayer = _dataLayer;
        this.namespace = namespace;
    }
    key() {
        return `${PKCE_VERIFIER_STORAGE_KEY}:${this.namespace}`;
    }
    async startPKCETransaction() {
        const keyPair = await PKCEManager.createProofkeyPair();
        this._dataLayer.setItem(this.key(), JSON.stringify(keyPair));
        return keyPair;
    }
    getPKPair() {
        const serialized = this._dataLayer.getItem(this.key());
        if (serialized === null) {
            return undefined;
        }
        try {
            return JSON.parse(serialized);
        } catch  {
            logger.warn('Found malformed Proof Key pair in localstorage.');
            return undefined;
        }
    }
    clearPKPair() {
        return this._dataLayer.removeItem(this.key());
    }
    static async createProofkeyPair() {
        const bytes = new Uint32Array(16);
        window.crypto.getRandomValues(bytes);
        const codeVerifier = Array.from(bytes).map(toHex).join('');
        const digest = await window.crypto.subtle.digest('SHA-256', new TextEncoder().encode(codeVerifier));
        return {
            code_challenge: base64URLEncode(digest),
            code_verifier: codeVerifier
        };
    }
}

export { PKCEManager };
//# sourceMappingURL=PKCEManager.mjs.map
