"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validatePhoneProviderConfig = validatePhoneProviderConfig;
exports.validateApplicationProviderConfig = validateApplicationProviderConfig;
function isNonEmptyString(value) {
    return typeof value === 'string' && value.trim().length > 0;
}
/**
 * Validates phone (voice call) application provider config on create/update.
 */
function validatePhoneProviderConfig(provider) {
    if (!provider || typeof provider !== 'object') {
        return 'Phone provider configuration is required.';
    }
    const config = provider;
    const number = isNonEmptyString(config.number) ? config.number.trim() : '';
    const url = isNonEmptyString(config.url) ? config.url.trim() : '';
    let connectionMode = config.connectionMode;
    if (connectionMode !== 'number' && connectionMode !== 'websocket') {
        connectionMode = url ? 'websocket' : 'number';
    }
    if (connectionMode === 'number') {
        if (!number) {
            return 'Phone number is required for number-based voice applications.';
        }
        if (!/^(\+\d{8,15})$/.test(number)) {
            return 'Phone number must be in E.164 format (e.g. +1234567890).';
        }
        return null;
    }
    if (!url) {
        return 'WebSocket URL is required for WebSocket-based voice applications.';
    }
    try {
        const parsed = new URL(url);
        if (parsed.protocol !== 'ws:' && parsed.protocol !== 'wss:') {
            return 'WebSocket URL must use ws:// or wss://';
        }
    }
    catch {
        return 'WebSocket URL is invalid.';
    }
    const auth = isNonEmptyString(config.auth) ? config.auth.trim() : 'none';
    if (auth === 'api_key') {
        const apiKey = config.apiKey;
        if (!isNonEmptyString(apiKey?.key) || !isNonEmptyString(apiKey?.value)) {
            return 'API key name and value are required when auth is api_key.';
        }
    }
    else if (auth === 'basic') {
        if (!isNonEmptyString(config.username) || !isNonEmptyString(config.password)) {
            return 'Username and password are required when auth is basic.';
        }
    }
    else if (auth === 'custom') {
        const fields = Array.isArray(config.customFields) ? config.customFields : [];
        const valid = fields.filter((row) => row &&
            typeof row === 'object' &&
            isNonEmptyString(row.key));
        if (valid.length === 0) {
            return 'At least one custom auth header is required when auth is custom.';
        }
    }
    if (Array.isArray(config.headers)) {
        for (const row of config.headers) {
            if (!row || typeof row !== 'object') {
                continue;
            }
            const key = row.key;
            const value = row.value;
            const keyStr = isNonEmptyString(key) ? key.trim() : '';
            const valueStr = isNonEmptyString(value) ? value.trim() : '';
            if (keyStr && !valueStr) {
                return 'Header values are required when a header key is set.';
            }
            if (!keyStr && valueStr) {
                return 'Header keys are required when a header value is set.';
            }
        }
    }
    return null;
}
function validateApplicationProviderConfig(providerConfig) {
    if (!providerConfig || typeof providerConfig !== 'object') {
        return null;
    }
    const pc = providerConfig;
    if (pc.provider_type !== 'phone') {
        return null;
    }
    const config = pc.config;
    const provider = config?.provider;
    return validatePhoneProviderConfig(provider);
}
//# sourceMappingURL=validatePhoneProviderConfig.js.map