"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSmartPerfettoVersion = getSmartPerfettoVersion;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
let cachedVersion = null;
function getSmartPerfettoVersion() {
    if (cachedVersion)
        return cachedVersion;
    const packageJsonPath = findBackendPackageJson();
    if (!packageJsonPath) {
        cachedVersion = '0.0.0';
        return cachedVersion;
    }
    try {
        const packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, 'utf-8'));
        cachedVersion = typeof packageJson.version === 'string' ? packageJson.version : '0.0.0';
    }
    catch {
        cachedVersion = '0.0.0';
    }
    return cachedVersion;
}
function findBackendPackageJson() {
    let dir = __dirname;
    for (let i = 0; i < 4; i++) {
        const packageJsonPath = path_1.default.join(dir, 'package.json');
        if (fs_1.default.existsSync(packageJsonPath) && isSmartPerfettoBackendPackage(packageJsonPath)) {
            return packageJsonPath;
        }
        const parent = path_1.default.dirname(dir);
        if (parent === dir)
            break;
        dir = parent;
    }
    return null;
}
function isSmartPerfettoBackendPackage(packageJsonPath) {
    try {
        const packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, 'utf-8'));
        return packageJson.name === '@gracker/smartperfetto';
    }
    catch {
        return false;
    }
}
//# sourceMappingURL=version.js.map