/**
 * @fileoverview Runtime capability detection for multi-environment support.
 * Detects presence of Node features, Web/Workers APIs, and common globals.
 * @module src/utils/internal/runtime
 */
// Best-effort static detection without throwing in restricted envs
const safeHas = (key) => {
    try {
        // @ts-expect-error index access on globalThis
        return typeof globalThis[key] !== 'undefined';
    }
    catch {
        return false;
    }
};
/**
 * Safely checks if process.versions.node exists and is a string.
 * Uses try-catch to handle environments where property access might be restricted.
 */
const hasNodeVersion = () => {
    try {
        return (typeof process !== 'undefined' &&
            typeof process.versions === 'object' &&
            process.versions !== null &&
            typeof process.versions.node === 'string');
    }
    catch {
        return false;
    }
};
/**
 * Safely checks if globalThis.performance.now is a function.
 * Uses try-catch to handle environments where property access might be restricted.
 */
const hasPerformanceNowFunction = () => {
    try {
        const perf = globalThis.performance;
        return typeof perf === 'object' && perf !== null && typeof perf.now === 'function';
    }
    catch {
        return false;
    }
};
const isNode = hasNodeVersion();
const isBun = isNode && typeof process?.versions?.bun === 'string';
const hasProcess = typeof process !== 'undefined';
const hasBuffer = typeof Buffer !== 'undefined';
const hasTextEncoder = safeHas('TextEncoder');
const hasPerformanceNow = hasPerformanceNowFunction();
/**
 * Safely checks if WorkerGlobalScope exists.
 * Web Workers and some non-CF worker environments expose this; Cloudflare
 * Workers under `nodejs_compat` do not.
 */
const hasWorkerGlobalScope = () => {
    try {
        return 'WorkerGlobalScope' in globalThis;
    }
    catch {
        return false;
    }
};
/**
 * Safely checks if `navigator.userAgent === 'Cloudflare-Workers'`.
 * Canonical CF detection (works regardless of `nodejs_compat`) when the
 * `global_navigator` flag is enabled — auto-enabled on recent compat dates.
 */
const hasCloudflareWorkerNavigator = () => {
    try {
        return (globalThis.navigator?.userAgent ===
            'Cloudflare-Workers');
    }
    catch {
        return false;
    }
};
// `isWorkerLike` is independent of `isNode`: under `nodejs_compat` both are true.
const isWorkerLike = hasCloudflareWorkerNavigator() || hasWorkerGlobalScope();
const isBrowserLike = !isNode && !isWorkerLike && safeHas('window');
/**
 * Singleton snapshot of runtime capabilities for the current environment.
 * Computed once at module load; safe to read at any time without async.
 *
 * @example
 * ```typescript
 * import { runtimeCaps } from './runtime.js';
 *
 * if (runtimeCaps.hasBuffer) {
 *   // Node.js / Bun path — use Buffer for encoding
 * } else {
 *   // Worker / browser path — use Web APIs
 * }
 * ```
 */
export const runtimeCaps = {
    isNode,
    isBun,
    isWorkerLike,
    isBrowserLike,
    hasProcess,
    hasBuffer,
    hasTextEncoder,
    hasPerformanceNow,
};
//# sourceMappingURL=runtime.js.map