import { resolveEnvPlaceholders } from '../env.js';
const ENV_PLACEHOLDER_PATTERN = /\$\{[A-Za-z_][A-Za-z0-9_]*\}/;
export function resolveCommandArgument(value) {
    if (!value) {
        return value;
    }
    if (!value.includes('$')) {
        return value;
    }
    const needsInterpolation = value.startsWith('$env:') || ENV_PLACEHOLDER_PATTERN.test(value);
    if (!needsInterpolation) {
        return value;
    }
    return resolveEnvPlaceholders(value);
}
export function resolveCommandArguments(args) {
    if (!args || args.length === 0) {
        return [];
    }
    return args.map((arg) => resolveCommandArgument(arg));
}
export function normalizeTimeout(raw) {
    if (raw == null) {
        return undefined;
    }
    if (!Number.isFinite(raw)) {
        return undefined;
    }
    const coerced = Math.trunc(raw);
    return coerced > 0 ? coerced : undefined;
}
export function raceWithTimeout(promise, timeoutMs) {
    return new Promise((resolve, reject) => {
        const timer = setTimeout(() => {
            // Reject with a Timeout error; higher-level catch blocks decide whether to recycle the transport.
            reject(new Error('Timeout'));
        }, timeoutMs);
        promise.then((value) => {
            clearTimeout(timer);
            resolve(value);
        }, (error) => {
            clearTimeout(timer);
            reject(error);
        });
    });
}
//# sourceMappingURL=utils.js.map