function validateTextField(field, raw) {
    if (raw === undefined || raw === null || raw === '') {
        if (field.required) {
            return `"${field.label}" is required`;
        }
        return undefined;
    }
    if (typeof raw !== 'string') {
        return `"${field.label}" must be a string`;
    }
    if (field.maxLength !== undefined && raw.length > field.maxLength) {
        return `"${field.label}" must not exceed ${field.maxLength} characters`;
    }
    return undefined;
}
function validateNumberField(field, raw) {
    if (raw === undefined || raw === null || raw === '') {
        if (field.required) {
            return `"${field.label}" is required`;
        }
        return undefined;
    }
    const num = typeof raw === 'string' ? Number(raw) : raw;
    if (typeof num !== 'number' || isNaN(num)) {
        return `"${field.label}" must be a number`;
    }
    if (field.min !== undefined && num < field.min) {
        return `"${field.label}" must be at least ${field.min}`;
    }
    if (field.max !== undefined && num > field.max) {
        return `"${field.label}" must be at most ${field.max}`;
    }
    return undefined;
}
function validateBooleanField(field, raw) {
    if (raw === undefined || raw === null) {
        if (field.required) {
            return `"${field.label}" is required`;
        }
        return undefined;
    }
    if (typeof raw !== 'boolean' && raw !== 'true' && raw !== 'false') {
        return `"${field.label}" must be a boolean`;
    }
    return undefined;
}
function validateEnumField(field, raw) {
    if (raw === undefined || raw === null || raw === '') {
        if (field.required) {
            return `"${field.label}" is required`;
        }
        return undefined;
    }
    if (typeof raw !== 'string') {
        return `"${field.label}" must be a string`;
    }
    if (!field.options.includes(raw)) {
        return `"${field.label}" must be one of: ${field.options.join(', ')}`;
    }
    return undefined;
}
function validateMultiSelectField(field, raw) {
    if (raw === undefined || raw === null) {
        if (field.required) {
            return `"${field.label}" is required`;
        }
        return undefined;
    }
    const arr = Array.isArray(raw) ? raw : [raw];
    const invalid = arr.filter((v) => !field.options.includes(String(v)));
    if (invalid.length > 0) {
        return `"${field.label}" contains invalid options: ${invalid.join(', ')}`;
    }
    return undefined;
}
function validateField(field, raw) {
    switch (field.type) {
        case 'text':
            return validateTextField(field, raw);
        case 'number':
            return validateNumberField(field, raw);
        case 'boolean':
            return validateBooleanField(field, raw);
        case 'enum':
            return validateEnumField(field, raw);
        case 'multi-select':
            return validateMultiSelectField(field, raw);
    }
}
function coerceToString(raw) {
    if (typeof raw === 'string') {
        return raw;
    }
    if (typeof raw === 'number' || typeof raw === 'boolean') {
        return String(raw);
    }
    return undefined;
}
function coerceToNumber(raw) {
    const n = typeof raw === 'string' ? Number(raw) : raw;
    return typeof n === 'number' && !isNaN(n) ? n : undefined;
}
function coerceToBoolean(raw) {
    if (typeof raw === 'boolean') {
        return raw;
    }
    if (raw === 'true') {
        return true;
    }
    if (raw === 'false') {
        return false;
    }
    return undefined;
}
function coerceToStringArray(raw) {
    if (Array.isArray(raw)) {
        return raw;
    }
    const str = coerceToString(raw);
    return str !== undefined ? [str] : undefined;
}
function coerceValue(field, raw) {
    if (raw === undefined || raw === null) {
        return undefined;
    }
    switch (field.type) {
        case 'text':
        case 'enum':
            return coerceToString(raw);
        case 'number':
            return coerceToNumber(raw);
        case 'boolean':
            return coerceToBoolean(raw);
        case 'multi-select':
            return coerceToStringArray(raw);
    }
}
/**
 * Parse and validate a raw client response against an elicitation request schema.
 *
 * Returns a typed result indicating whether all fields are valid and providing
 * coerced values or per-field error messages.
 */
export function parseElicitationResponse(request, raw) {
    const errors = {};
    const values = {};
    for (const field of request.fields) {
        const rawValue = raw[field.key];
        const error = validateField(field, rawValue);
        if (error) {
            errors[field.key] = error;
        }
        else {
            const coerced = coerceValue(field, rawValue);
            if (coerced !== undefined) {
                values[field.key] = coerced;
            }
        }
    }
    const valid = Object.keys(errors).length === 0;
    return valid ? { valid: true, values, errors } : { valid: false, errors };
}
//# sourceMappingURL=response-parser.js.map