"use strict";
exports.__esModule = true;
exports.validate = exports.parse = void 0;
var DATA_URL_REGEX = 
// eslint-disable-next-line unicorn/no-unsafe-regex
/^data:([a-z]+\/[a-z0-9-+.]+(;[a-z0-9-.!#$%*+.{}|~`]+=[a-z0-9-.!#$%*+.{}()_|~`]+)*)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@/?%\s<>]*?)$/i;
/**
 * Determine if a given data URL is valid or not.
 *
 * @see {@link https://developer.mozilla.org/en-US/docs/data_URIs}
 * @see {@link http://tools.ietf.org/html/rfc2397}
 * @see {@link http://tools.ietf.org/html/rfc2396#section2}
 */
function validate(str) {
    return DATA_URL_REGEX.test((str || '').trim());
}
exports.validate = validate;
/**
 * Parse a given data URL into its individual parts.
 *
 */
function parse(str) {
    if (!validate(str)) {
        return false;
    }
    var parts = str.trim().match(DATA_URL_REGEX);
    var parsed = {};
    if (parts[1]) {
        parsed.mediaType = parts[1].toLowerCase();
        var mediaTypeParts = parts[1].split(';').map(function (x) {
            // `name` attributes are for filenames so we shouldn't lowercase them as some filesystems are
            // case-sensitive.
            if (x.startsWith('name=')) {
                return x;
            }
            return x.toLowerCase();
        });
        parsed.contentType = mediaTypeParts[0];
        mediaTypeParts.slice(1).forEach(function (attribute) {
            var p = attribute.split('=');
            parsed[p[0]] = p[1];
        });
    }
    parsed.base64 = !!parts[parts.length - 2];
    parsed.data = parts[parts.length - 1] || '';
    parsed.toBuffer = function () {
        var encoding = parsed.base64 ? 'base64' : 'utf8';
        return Buffer.from(parsed.data, encoding);
    };
    return parsed;
}
exports.parse = parse;
