"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.base64url = void 0;
const encode = (input, encoding = "utf8") => {
    if (!Buffer.isBuffer(input)) {
        input = Buffer.from(input, encoding);
    }
    if (Buffer.isEncoding('base64url')) {
        return input.toString('base64url');
    }
    return input.toString("base64")
        .replace(/=/g, "")
        .replace(/\+/g, "-")
        .replace(/\//g, "_");
};
const decode = (base64Url, encoding = "utf8") => {
    if (Buffer.isEncoding('base64url')) {
        return Buffer.from(base64Url, 'base64url').toString(encoding);
    }
    return Buffer.from(toBase64(base64Url), 'base64').toString(encoding);
};
const padString = (str) => {
    const padLength = 4 - (str.length % 4);
    if (padLength < 4) {
        str += '='.repeat(padLength);
    }
    return str;
};
const toBase64 = (base64Url) => {
    if (Buffer.isEncoding('base64url')) {
        return Buffer.from(base64Url, 'base64url').toString('base64');
    }
    return padString(base64Url)
        .replace(/-/g, "+")
        .replace(/_/g, "/");
};
const fromBase64 = (base64) => {
    return encode(base64, 'base64');
};
const fromArrayBuffer = (buf) => {
    if (!buf) {
        return '';
    }
    return encode(Buffer.from(new Uint8Array(buf)));
};
const toBuffer = (base64Url) => {
    if (Buffer.isEncoding('base64url')) {
        return Buffer.from(base64Url, 'base64url');
    }
    return Buffer.from(toBase64(base64Url), 'base64');
};
exports.base64url = encode;
exports.base64url.encode = encode;
exports.base64url.decode = decode;
exports.base64url.toBase64 = toBase64;
exports.base64url.fromBase64 = fromBase64;
exports.base64url.toBuffer = toBuffer;
exports.base64url.fromArrayBuffer = fromArrayBuffer;
//# sourceMappingURL=base64url.js.map