"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cacheGroup = void 0;
const mergeRegex = (r1, r2) => {
    if (!r2) {
        return r1;
    }
    return new RegExp(`${r1.source}|${r2.source}`);
};
const isRegExp = (a) => typeof a.exec === 'function';
const getRegexFromCacheGroup = (name, optimization) => {
    if (!optimization ||
        !optimization.splitChunks ||
        !optimization.splitChunks.cacheGroups ||
        typeof optimization.splitChunks.cacheGroups === 'string' ||
        typeof optimization.splitChunks.cacheGroups === 'function' ||
        isRegExp(optimization.splitChunks.cacheGroups)) {
        return;
    }
    const cacheGroup = optimization.splitChunks.cacheGroups[name];
    if (!cacheGroup) {
        return;
    }
    if (isRegExp(cacheGroup.test)) {
        return cacheGroup.test;
    }
};
exports.cacheGroup = (name, regexp) => (_, util) => (prevConfig) => {
    const previousRegexp = getRegexFromCacheGroup(name, prevConfig.optimization);
    return util.merge({
        optimization: {
            splitChunks: {
                cacheGroups: {
                    [name]: {
                        test: mergeRegex(regexp, previousRegexp),
                        reuseExistingChunk: true,
                        name,
                        chunks: 'all',
                        enforce: true,
                    },
                },
            },
        },
    })(prevConfig);
};
