"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkForUpdate = checkForUpdate;
exports.getCurrentVersion = getCurrentVersion;
const https_1 = __importDefault(require("https"));
function getLocalVersion() {
    try {
        return require('../package.json').version;
    }
    catch {
        return '0.0.0';
    }
}
function fetchLatestVersion(packageName) {
    return new Promise((resolve) => {
        const req = https_1.default.get(`https://registry.npmjs.org/${packageName}/latest`, { timeout: 5000, headers: { 'Accept': 'application/json' } }, (res) => {
            if (res.statusCode !== 200) {
                res.resume();
                resolve(null);
                return;
            }
            let body = '';
            res.on('data', (chunk) => { body += chunk; });
            res.on('end', () => {
                try {
                    const data = JSON.parse(body);
                    resolve(data.version ?? null);
                }
                catch {
                    resolve(null);
                }
            });
        });
        req.on('error', () => resolve(null));
        req.on('timeout', () => { req.destroy(); resolve(null); });
    });
}
function compareVersions(a, b) {
    const pa = a.split('.').map(Number);
    const pb = b.split('.').map(Number);
    for (let i = 0; i < 3; i++) {
        const diff = (pa[i] || 0) - (pb[i] || 0);
        if (diff !== 0)
            return diff;
    }
    return 0;
}
async function checkForUpdate() {
    const currentVersion = getLocalVersion();
    const latestVersion = await fetchLatestVersion('splitgame');
    return {
        currentVersion,
        latestVersion,
        updateAvailable: latestVersion !== null && compareVersions(latestVersion, currentVersion) > 0,
    };
}
function getCurrentVersion() {
    return getLocalVersion();
}
//# sourceMappingURL=version-checker.js.map