"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitTarget = exports.gitRules = void 0;
const tslib_1 = require("tslib");
const github_url_from_git_1 = tslib_1.__importDefault(require("github-url-from-git"));
const zod_1 = require("zod");
const regex_1 = require("../../../../util/regex");
const github_releases_1 = require("../../../datasource/github-releases");
const githubUrlRegex = (0, regex_1.regEx)(/^https:\/\/github\.com\/(?<packageName>[^/]+\/[^/]+)/);
function githubPackageName(input) {
    return (0, github_url_from_git_1.default)(input)?.match(githubUrlRegex)?.groups?.packageName;
}
exports.gitRules = ['git_repository'];
exports.GitTarget = zod_1.z
    .object({
    rule: zod_1.z.enum(exports.gitRules),
    name: zod_1.z.string(),
    tag: zod_1.z.string().optional(),
    commit: zod_1.z.string().optional(),
    remote: zod_1.z.string(),
})
    .refine(({ tag, commit }) => !!tag || !!commit)
    .transform(({ rule, name, tag, commit, remote }) => {
    const dep = {
        depType: rule,
        depName: name,
    };
    if (tag) {
        dep.currentValue = tag;
    }
    if (commit) {
        dep.currentDigest = commit;
    }
    const githubPackage = githubPackageName(remote);
    if (githubPackage) {
        dep.datasource = github_releases_1.GithubReleasesDatasource.id;
        dep.packageName = githubPackage;
    }
    if (!dep.datasource) {
        dep.skipReason = 'unsupported-datasource';
    }
    return dep;
});
//# sourceMappingURL=git.js.map