"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVersionCacheBust = exports.getVersionNumber = exports.getVersion = exports.getGitCommit = void 0;
const crypto_1 = __importDefault(require("crypto"));
const child_process_1 = require("child_process");
const package_json_1 = __importDefault(require("../package.json"));
let _gitCommit;
let _gitCommitFetched = false;
function getGitCommit() {
    if (_gitCommitFetched) {
        return _gitCommit ?? null;
    }
    _gitCommitFetched = true;
    try {
        _gitCommit = (0, child_process_1.execSync)("git rev-parse --short HEAD", {
            encoding: "utf-8",
            timeout: 2000,
            stdio: ["ignore", "pipe", "ignore"],
        }).trim();
    }
    catch {
        _gitCommit = null;
    }
    return _gitCommit;
}
exports.getGitCommit = getGitCommit;
function getVersion() {
    const gitCommit = getGitCommit();
    const version = `v${package_json_1.default.version}`;
    return gitCommit ? `source (${gitCommit} / ${version})` : version;
}
exports.getVersion = getVersion;
function getVersionNumber() {
    return package_json_1.default.version;
}
exports.getVersionNumber = getVersionNumber;
let _cacheBust;
function getVersionCacheBust() {
    if (!_cacheBust) {
        const hash = crypto_1.default.createHash("sha256").update(getVersion()).digest("hex");
        _cacheBust = hash.substring(0, 10);
    }
    return _cacheBust;
}
exports.getVersionCacheBust = getVersionCacheBust;
