import { hasBuffer } from '../Platform.js';
/**
 * Converts base64 string to Uint8Array.
 *
 * @returns The restored array view.
 */
export function base64ToBuffer(str) {
    if (hasBuffer) {
        return Buffer.from(str, 'base64');
    }
    const asciiString = atob(str);
    return new Uint8Array([...asciiString].map((char) => char.charCodeAt(0)));
}
function uint8ArrayToBase64(view) {
    if (hasBuffer) {
        const buffer = Buffer.from(view);
        return buffer.toString('base64');
    }
    return btoa(String.fromCharCode(...view));
}
function arrayBufferToBase64(buffer) {
    if (hasBuffer) {
        const nodeBuffer = Buffer.from(buffer);
        return nodeBuffer.toString('base64');
    }
    return btoa(String.fromCharCode(...new Uint8Array(buffer)));
}
/**
 * Converts incoming data to base64 string.
 *
 * @returns Safe to store string.
 */
export function bufferToBase64(view) {
    const name = view[Symbol.toStringTag];
    if (name === 'Uint8Array') {
        return uint8ArrayToBase64(view);
    }
    if (name === 'ArrayBuffer') {
        return arrayBufferToBase64(view);
    }
    const buffer = view;
    return buffer.toString('base64');
}
/**
 * Reads file as ArrayBuffer
 */
export async function fileToBuffer(blob) {
    const ab = await blob.arrayBuffer();
    return new Uint8Array(ab);
}
/**
 * Reads file as string
 */
export async function fileToString(blob) {
    return blob.text();
}
//# sourceMappingURL=Buffer.js.map