import { coerceToBoolean, coerceToInteger, coerceToNumber, coerceToString } from '@takeshape/util';
/**
 * Coerces a value to match the type specified in a PropertySchema only supporting primitive types
 * @param schema - The PropertySchema object defining the expected type
 * @param value - The value to coerce
 * @returns The coerced value
 * @throws Error if coercion fails or type is not supported
 */
export function coercePrimitiveValue(schema, value) {
    // Handle undefined/null values
    if (value === undefined) {
        if (schema.default !== undefined) {
            return schema.default;
        }
    }
    const primaryType = Array.isArray(schema.type) ? schema.type[0] : schema.type;
    switch (primaryType) {
        case 'string':
            return coerceToString(value);
        case 'number':
            return coerceToNumber(value);
        case 'integer':
            return coerceToInteger(value);
        case 'boolean':
            return coerceToBoolean(value);
        case 'null':
            return null;
        default:
            // If no type specified, return as-is
            return value;
    }
}
