"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.configRegexPredicate = exports.isConfigRegex = exports.UUIDRegex = exports.anyMatchRegexOrMinimatch = exports.matchRegexOrMinimatch = exports.makeRegexOrMinimatchPredicate = exports.isDockerDigest = void 0;
const tslib_1 = require("tslib");
const is_1 = tslib_1.__importDefault(require("@sindresorhus/is"));
const minimatch_1 = require("./minimatch");
const regex_1 = require("./regex");
function isDockerDigest(input) {
    return /^sha256:[a-f0-9]{64}$/i.test(input);
}
exports.isDockerDigest = isDockerDigest;
function makeRegexOrMinimatchPredicate(pattern) {
    if (pattern.length > 2 && pattern.startsWith('/') && pattern.endsWith('/')) {
        try {
            const regex = (0, regex_1.regEx)(pattern.slice(1, -1));
            return (x) => regex.test(x);
        }
        catch (err) {
            return null;
        }
    }
    const mm = (0, minimatch_1.minimatch)(pattern, { dot: true });
    return (x) => mm.match(x);
}
exports.makeRegexOrMinimatchPredicate = makeRegexOrMinimatchPredicate;
function matchRegexOrMinimatch(input, pattern) {
    const predicate = makeRegexOrMinimatchPredicate(pattern);
    return predicate ? predicate(input) : false;
}
exports.matchRegexOrMinimatch = matchRegexOrMinimatch;
function anyMatchRegexOrMinimatch(input, patterns) {
    return patterns.some((pattern) => matchRegexOrMinimatch(input, pattern));
}
exports.anyMatchRegexOrMinimatch = anyMatchRegexOrMinimatch;
exports.UUIDRegex = (0, regex_1.regEx)(/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i);
const configValStart = (0, regex_1.regEx)(/^!?\//);
const configValEnd = (0, regex_1.regEx)(/\/i?$/);
function isConfigRegex(input) {
    return (is_1.default.string(input) && configValStart.test(input) && configValEnd.test(input));
}
exports.isConfigRegex = isConfigRegex;
function parseConfigRegex(input) {
    try {
        const regexString = input
            .replace(configValStart, '')
            .replace(configValEnd, '');
        return input.endsWith('i') ? (0, regex_1.regEx)(regexString, 'i') : (0, regex_1.regEx)(regexString);
    }
    catch (err) {
        // no-op
    }
    return null;
}
function configRegexPredicate(input) {
    if (isConfigRegex(input)) {
        const configRegex = parseConfigRegex(input);
        if (configRegex) {
            const isPositive = !input.startsWith('!');
            return (x) => {
                const res = configRegex.test(x);
                return isPositive ? res : !res;
            };
        }
    }
    return null;
}
exports.configRegexPredicate = configRegexPredicate;
//# sourceMappingURL=string-match.js.map