"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.findInvalidationGroup = exports.replaceUrlByGroupRegex = exports.getGroupFilename = exports.getGroupS3Key = exports.INVALIDATION_DATA_DIR = void 0;
const _ = __importStar(require("../lodash"));
const console_1 = require("../console");
const build_1 = require("../../build");
exports.INVALIDATION_DATA_DIR = "/_invalidation_group_data/";
function getGroupS3Key(basicGroup, resource) {
    return `${(resource.getBasePath() || "").replace(/^\//, "")}${!resource.getBasePath() ? "" : "/"}_next/data/${build_1.DEFAULT_BUILD_ID}${exports.INVALIDATION_DATA_DIR}${getGroupFilename(basicGroup)}`;
}
exports.getGroupS3Key = getGroupS3Key;
function getGroupFilename(basicGroup) {
    const filename = `${basicGroup.invalidationPath}${basicGroup.maxAccessNumber}`.replace(/[^a-z0-9A-Z]/g, "_");
    return `${filename}.json`;
}
exports.getGroupFilename = getGroupFilename;
function replaceUrlByGroupRegex(group, url) {
    return url.replace(new RegExp(group.regex), group.invalidationPath);
}
exports.replaceUrlByGroupRegex = replaceUrlByGroupRegex;
function findInvalidationGroup(url, basicGroups) {
    console_1.debug(`[findInvalidationGroup] url: ${url}`);
    if (_.isEmpty(basicGroups)) {
        console_1.debug(`[findInvalidationGroup] no group url`);
        return null;
    }
    let result = null;
    basicGroups === null || basicGroups === void 0 ? void 0 : basicGroups.forEach((group) => {
        console_1.debug(`[findInvalidationGroup] url match check: ${new RegExp(group.regex).test(url)}`);
        if (new RegExp(group.regex).test(url)) {
            result = group;
        }
    });
    return result;
}
exports.findInvalidationGroup = findInvalidationGroup;
