"use strict";
/*
 * @poppinss/utils
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.base64 = void 0;
/**
 * Helper class to base64 encode/decode values with option
 * for url encoding and decoding
 */
class Base64 {
    encode(data, encoding) {
        if (typeof data === 'string') {
            return Buffer.from(data, encoding).toString('base64');
        }
        return Buffer.from(data).toString('base64');
    }
    decode(encoded, encoding = 'utf-8', strict = false) {
        if (Buffer.isBuffer(encoded)) {
            return encoded.toString(encoding);
        }
        const decoded = Buffer.from(encoded, 'base64').toString(encoding);
        if (strict && this.encode(decoded, encoding) !== encoded) {
            return null;
        }
        return decoded;
    }
    urlEncode(data, encoding) {
        const encoded = typeof data === 'string' ? this.encode(data, encoding) : this.encode(data);
        return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
    }
    urlDecode(encoded, encoding = 'utf-8', strict = false) {
        if (Buffer.isBuffer(encoded)) {
            return encoded.toString(encoding);
        }
        const decoded = Buffer.from(encoded, 'base64').toString(encoding);
        if (strict && this.urlEncode(decoded, encoding) !== encoded) {
            return null;
        }
        return decoded;
    }
}
exports.base64 = new Base64();
