import { DefaultTraceConfig, traceConfigMetadata } from "./constants.js";
import { assertUnreachable, withSafety } from "../../utils/index.js";
const safelyParseInt = withSafety({ fn: parseInt });
/**
 * Parses an option based on its type
 * The order of precedence is: optionValue > envValue > defaultValue
 * @param key - The key of the option.
 * @param optionMetadata - The {@link TraceConfigOptionMetadata} for the option which includes its type, default value, and environment variable key.
 *
 */
function parseOption({ optionValue, optionMetadata, }) {
    if (optionValue !== undefined) {
        return optionValue;
    }
    const envValue = process.env[optionMetadata.envKey];
    if (envValue !== undefined) {
        switch (optionMetadata.type) {
            case "number": {
                const maybeEnvNumber = safelyParseInt(envValue);
                return maybeEnvNumber != null && !isNaN(maybeEnvNumber)
                    ? maybeEnvNumber
                    : optionMetadata.default;
            }
            case "boolean":
                return envValue.toLowerCase() === "true";
            default:
                assertUnreachable(optionMetadata);
        }
    }
    return optionMetadata.default;
}
/**
 * Generates a full trace config object based on passed in options, environment variables, and default values.
 * The order of precedence is: optionValue > envValue > defaultValue
 * @param options - The user provided TraceConfigOptions.
 * @returns A full TraceConfig object with all options set to their final values.
 */
export function generateTraceConfig(options) {
    if (options == null) {
        return DefaultTraceConfig;
    }
    return Object.entries(traceConfigMetadata).reduce((config, [key, optionMetadata]) => {
        const TraceConfigKey = key;
        return {
            ...config,
            [TraceConfigKey]: parseOption({
                optionValue: options[TraceConfigKey],
                optionMetadata,
            }),
        };
    }, {});
}
//# sourceMappingURL=traceConfig.js.map