import os from 'os';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const packageVersion = (name) => {
    try {
        return require(`${name}/package.json`).version;
    }
    catch {
        return undefined;
    }
};
const gitnexusVersion = () => {
    try {
        return require('../../../package.json').version;
    }
    catch {
        return 'unknown';
    }
};
const parsePositiveInt = (value, fallback) => {
    if (value === undefined)
        return fallback;
    const parsed = Number(value);
    return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback;
};
export const DEFAULT_EXACT_SCAN_LIMIT = 10_000;
export const getExactScanLimit = () => parsePositiveInt(process.env.GITNEXUS_SEMANTIC_EXACT_SCAN_LIMIT, DEFAULT_EXACT_SCAN_LIMIT);
export const getRuntimeFingerprint = () => ({
    platform: process.platform,
    arch: process.arch,
    node: process.version,
    gitnexus: gitnexusVersion(),
    ladybugdb: packageVersion('@ladybugdb/core'),
    onnxruntime: packageVersion('onnxruntime-node'),
});
export const isVectorExtensionSupportedByPlatform = (platform = process.platform) => platform !== 'win32';
export const getRuntimeCapabilities = () => {
    const vector = isVectorExtensionSupportedByPlatform() ? 'available' : 'unavailable';
    const exactScanLimit = getExactScanLimit();
    return {
        graph: 'available',
        fts: 'available',
        vector,
        semanticMode: vector === 'available' ? 'vector-index' : 'exact-scan',
        exactScanLimit,
        reason: vector === 'unavailable'
            ? 'LadybugDB VECTOR is disabled on this platform; semantic search uses exact scan when embeddings exist.'
            : undefined,
    };
};
export const defaultEmbeddingThreads = () => {
    const available = typeof os.availableParallelism === 'function' ? os.availableParallelism() : os.cpus().length;
    return Math.max(1, Math.min(4, Math.floor(available / 2) || 1));
};
