"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.REGEXP_LITERAL_PATTERN = void 0;
exports.extractFromRegexLiteral = extractFromRegexLiteral;
exports.parseRegexPattern = parseRegexPattern;
exports.REGEXP_LITERAL_PATTERN = /^\/(.*)\/([dgimsuvy]*)$/;
function extractFromRegexLiteral(regexString) {
    const matches = regexString.match(exports.REGEXP_LITERAL_PATTERN);
    if (!matches) {
        throw new Error(`Invalid regex format. Expected format: /pattern/flags. Received: ${regexString}`);
    }
    return {
        pattern: matches[1],
        flags: matches[2],
    };
}
function parseRegexPattern(pattern, options = {}) {
    const trimmed = pattern.trim();
    const { defaultFlags = '', forceFlags = '' } = options;
    if (exports.REGEXP_LITERAL_PATTERN.test(trimmed)) {
        const { pattern: extracted, flags } = extractFromRegexLiteral(trimmed);
        const merged = mergeFlags(flags, forceFlags);
        return new RegExp(extracted, merged);
    }
    const merged = mergeFlags(defaultFlags, forceFlags);
    return new RegExp(trimmed, merged);
}
function mergeFlags(base, extra) {
    if (!extra) {
        return base;
    }
    return [...new Set([...base, ...extra])].join('');
}
