function normalizeEnvSegment(segment) {
    return segment
        .replace(/([a-z0-9])([A-Z])/g, '$1_$2')
        .replace(/[^a-zA-Z0-9]+/g, '_')
        .replace(/_+/g, '_')
        .replace(/^_|_$/g, '')
        .toUpperCase();
}
export function envVarNameFromPath(path) {
    const key = path
        .split('.')
        .map(normalizeEnvSegment)
        .filter((segment) => segment.length > 0)
        .join('_');
    return `TAKT_${key}`;
}
export function parseEnvValue(envKey, raw, type) {
    if (type === 'string') {
        return raw;
    }
    if (type === 'boolean') {
        const normalized = raw.trim().toLowerCase();
        if (normalized === 'true')
            return true;
        if (normalized === 'false')
            return false;
        throw new Error(`${envKey} must be one of: true, false`);
    }
    if (type === 'number') {
        const trimmed = raw.trim();
        const value = Number(trimmed);
        if (!Number.isFinite(value)) {
            throw new Error(`${envKey} must be a number`);
        }
        return value;
    }
    try {
        return JSON.parse(raw);
    }
    catch {
        throw new Error(`${envKey} must be valid JSON`);
    }
}
export function setNestedConfigValue(target, path, value) {
    const parts = path.split('.');
    let current = target;
    for (let i = 0; i < parts.length - 1; i++) {
        const part = parts[i];
        if (!part)
            continue;
        const next = current[part];
        if (typeof next !== 'object' || next === null || Array.isArray(next)) {
            current[part] = {};
        }
        current = current[part];
    }
    const leaf = parts[parts.length - 1];
    if (!leaf)
        return;
    current[leaf] = value;
}
//# sourceMappingURL=config-env-shared.js.map