/** SDK-owned platform module. This implementation is maintained in goodvibes-sdk. */
const OK = { valid: true, violations: [] };
function fail(...messages) {
    return { valid: false, violations: messages };
}
export function isString(v) {
    return typeof v === 'string';
}
export function isNumber(v) {
    return typeof v === 'number';
}
export function isBoolean(v) {
    return typeof v === 'boolean';
}
export function isObject(v) {
    return v !== null && typeof v === 'object' && !Array.isArray(v);
}
export function validateEventFields(type, v, fields) {
    if (!isObject(v))
        return fail('event must be an object');
    if (v['type'] !== type)
        return fail(`type must be '${type}', got ${String(v['type'])}`);
    const violations = [];
    for (const field of fields) {
        const value = v[field.key];
        switch (field.kind) {
            case 'string':
                if (!isString(value))
                    violations.push(`${field.key} must be a string`);
                break;
            case 'number':
                if (!isNumber(value))
                    violations.push(`${field.key} must be a number`);
                break;
            case 'boolean':
                if (!isBoolean(value))
                    violations.push(`${field.key} must be a boolean`);
                break;
            case 'string[]':
                if (!Array.isArray(value) || value.some((item) => !isString(item))) {
                    violations.push(`${field.key} must be an array of strings`);
                }
                break;
            case 'enum':
                if (!isString(value) || !(field.values ?? []).includes(value)) {
                    violations.push(`${field.key} must be one of: ${(field.values ?? []).join(', ')}`);
                }
                break;
            case 'object':
                if (!isObject(value))
                    violations.push(`${field.key} must be an object`);
                break;
        }
    }
    return violations.length ? { valid: false, violations } : OK;
}
export function validateEnvelope(v) {
    if (!isObject(v))
        return fail('envelope must be an object');
    const violations = [];
    if (!isString(v['traceId']))
        violations.push('traceId must be a string');
    if (!isString(v['sessionId']))
        violations.push('sessionId must be a string');
    if (!isNumber(v['timestamp']))
        violations.push('timestamp must be a number');
    if (!isString(v['source']))
        violations.push('source must be a string');
    if (!isObject(v['event']))
        violations.push('event must be an object');
    return violations.length ? { valid: false, violations } : OK;
}
