"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mavenRules = exports.RuleToMavenPackageDep = void 0;
exports.fillRegistryUrls = fillRegistryUrls;
const good_enough_parser_1 = require("good-enough-parser");
const zod_1 = require("zod");
const regex_1 = require("../../../../util/regex");
const maven_1 = require("../../../datasource/maven");
const gradle_1 = require("../../../versioning/gradle");
const fragments_1 = require("../fragments");
const common_1 = require("./common");
const artifactMethod = 'artifact';
const installMethod = 'install';
const commonDepType = 'maven_install';
const mavenVariableRegex = (0, regex_1.regEx)(/^maven.*/);
const bzlmodMavenMethods = [installMethod, artifactMethod];
const methodRegex = (0, regex_1.regEx)(`^${bzlmodMavenMethods.join('|')}$`);
function getParsedRuleByMethod(method) {
    return `maven_${method}`;
}
const ArtifactSpec = zod_1.z.object({
    group: zod_1.z.string(),
    artifact: zod_1.z.string(),
    version: zod_1.z.string(),
});
const MavenArtifactTarget = fragments_1.RecordFragmentSchema.extend({
    children: zod_1.z.object({
        rule: fragments_1.StringFragmentSchema.extend({
            value: zod_1.z.literal(getParsedRuleByMethod(artifactMethod)),
        }),
        artifact: fragments_1.StringFragmentSchema,
        group: fragments_1.StringFragmentSchema,
        version: fragments_1.StringFragmentSchema,
    }),
}).transform(({ children: { rule, artifact, group, version } }) => [
    {
        datasource: maven_1.MavenDatasource.id,
        versioning: gradle_1.id,
        depName: `${group.value}:${artifact.value}`,
        currentValue: version.value,
        depType: rule.value,
    },
]);
const MavenInstallTarget = fragments_1.RecordFragmentSchema.extend({
    children: zod_1.z.object({
        rule: fragments_1.StringFragmentSchema.extend({
            value: zod_1.z.literal(getParsedRuleByMethod(installMethod)),
        }),
        artifacts: fragments_1.StringArrayFragmentSchema.transform((artifacts) => {
            const result = [];
            for (const { value } of artifacts.items) {
                const [group, artifact, version] = value.split(':');
                if (group && artifact && version) {
                    result.push({ group, artifact, version });
                }
            }
            return result;
        }),
        repositories: fragments_1.StringArrayFragmentSchema,
    }),
}).transform(({ children: { rule, artifacts, repositories } }) => artifacts.map(({ group, artifact, version: currentValue }) => ({
    datasource: maven_1.MavenDatasource.id,
    versioning: gradle_1.id,
    depName: `${group}:${artifact}`,
    currentValue,
    depType: rule.value,
    registryUrls: repositories.items.map((i) => i.value),
})));
exports.RuleToMavenPackageDep = zod_1.z.union([
    MavenArtifactTarget,
    MavenInstallTarget,
]);
function fillRegistryUrls(packageDeps) {
    const artifactRules = [];
    const registryUrls = [];
    const result = [];
    // registry urls are specified only in maven.install, not in maven.artifact
    packageDeps.flat().forEach((dep) => {
        if (dep.depType === getParsedRuleByMethod(installMethod)) {
            if (Array.isArray(dep.registryUrls)) {
                registryUrls.push(...dep.registryUrls);
                result.push(dep);
            }
        }
        else if (dep.depType === getParsedRuleByMethod(artifactMethod)) {
            artifactRules.push(dep);
        }
    });
    const uniqUrls = [...new Set(registryUrls)];
    for (const artifactRule of artifactRules) {
        artifactRule.registryUrls = uniqUrls;
        artifactRule.depType = commonDepType;
        result.push(artifactRule);
    }
    return result;
}
exports.mavenRules = good_enough_parser_1.query
    .sym(mavenVariableRegex, (ctx, token) => {
    return ctx.startRule(token.value);
})
    .op('.')
    .sym(methodRegex, (ctx, token) => {
    const rule = ctx.currentRecord.children.rule;
    if (rule.type === 'string') {
        rule.value = getParsedRuleByMethod(token.value);
    }
    return ctx;
})
    .join(good_enough_parser_1.query.tree({
    type: 'wrapped-tree',
    maxDepth: 1,
    search: common_1.kvParams,
    postHandler: (ctx) => ctx.endRule(),
}));
//# sourceMappingURL=maven.js.map