import { METRIC_FILE, VERSION } from '../CallingClient/constants';
import { ServiceIndicator } from '../common/types';
import { METRIC_EVENT, } from './types';
import log from '../Logger';
let metricManager;
class MetricManager {
    webex;
    deviceInfo;
    serviceIndicator;
    constructor(webex, indicator) {
        log.info('Initializing metric manager...', { file: METRIC_FILE });
        this.webex = webex;
        this.serviceIndicator = indicator;
    }
    submitConnectionMetrics(name, metricAction, type, downTimestamp, upTimestamp) {
        const metricData = {
            tags: {
                metricAction,
                device_id: this.deviceInfo?.device?.deviceId,
                service_indicator: this.serviceIndicator,
            },
            fields: {
                device_url: this.deviceInfo?.device?.clientDeviceUri,
                mobius_url: this.deviceInfo?.device?.uri,
                calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                downTimestamp,
                upTimestamp,
            },
            type,
        };
        if (metricData) {
            this.webex.internal.metrics.submitClientMetrics(name, metricData);
        }
    }
    submitUploadLogsMetric(name, action, type, trackingId, feedbackId, correlationId, stack, callId, broadworksCorrelationInfo) {
        let data;
        const commonData = {
            tags: {
                action,
                device_id: this.deviceInfo?.device?.deviceId,
                service_indicator: this.serviceIndicator,
            },
            fields: {
                device_url: this.deviceInfo?.device?.clientDeviceUri,
                mobius_url: this.deviceInfo?.device?.uri,
                calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                correlation_id: correlationId,
                broadworksCorrelationInfo,
                tracking_id: trackingId,
                feedback_id: feedbackId,
                call_id: callId,
            },
            type,
        };
        switch (name) {
            case METRIC_EVENT.UPLOAD_LOGS_SUCCESS: {
                data = commonData;
                break;
            }
            case METRIC_EVENT.UPLOAD_LOGS_FAILED: {
                data = {
                    ...commonData,
                    fields: {
                        ...commonData.fields,
                        error: stack,
                    },
                };
                break;
            }
        }
        if (data) {
            this.webex.internal.metrics.submitClientMetrics(name, data);
        }
    }
    setDeviceInfo(deviceInfo) {
        this.deviceInfo = deviceInfo;
    }
    submitRegionInfoMetric(name, metricAction, type, mobiusHost, clientRegion, countryCode, trackingId) {
        const data = {
            tags: {
                action: metricAction,
                device_id: this.deviceInfo?.device?.deviceId,
                service_indicator: ServiceIndicator.CALLING,
            },
            fields: {
                device_url: this.deviceInfo?.device?.clientDeviceUri,
                mobius_url: this.deviceInfo?.device?.uri,
                calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                mobius_host: mobiusHost,
                client_region: clientRegion,
                country_code: countryCode,
                tracking_id: trackingId,
            },
            type,
        };
        this.webex.internal.metrics.submitClientMetrics(name, data);
    }
    submitMobiusServersMetric(name, metricAction, type, mobiusServers, trackingId) {
        const data = {
            tags: {
                action: metricAction,
                device_id: this.deviceInfo?.device?.deviceId,
                service_indicator: ServiceIndicator.CALLING,
            },
            fields: {
                device_url: this.deviceInfo?.device?.clientDeviceUri,
                mobius_url: this.deviceInfo?.device?.uri,
                calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                primary_mobius_servers_region: mobiusServers.primary.region,
                primary_mobius_servers_uris: mobiusServers.primary.uris.join(','),
                backup_mobius_servers_region: mobiusServers.backup.region,
                backup_mobius_servers_uris: mobiusServers.backup.uris.join(','),
                tracking_id: trackingId,
            },
            type,
        };
        this.webex.internal.metrics.submitClientMetrics(name, data);
    }
    submitRegistrationMetric(name, metricAction, type, caller, serverType, trackingId, keepaliveCount, clientError) {
        let data;
        switch (name) {
            case METRIC_EVENT.REGISTRATION: {
                data = {
                    tags: {
                        action: metricAction,
                        device_id: this.deviceInfo?.device?.deviceId,
                        service_indicator: this.serviceIndicator,
                    },
                    fields: {
                        device_url: this.deviceInfo?.device?.clientDeviceUri,
                        mobius_url: this.deviceInfo?.device?.uri,
                        calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                        reg_source: caller,
                        server_type: serverType,
                        trackingId,
                    },
                    type,
                };
                break;
            }
            case METRIC_EVENT.REGISTRATION_ERROR: {
                let errorData;
                if (clientError) {
                    errorData = {
                        msg: clientError.getError().message,
                        type: clientError.getError().type,
                    };
                }
                data = {
                    tags: {
                        action: metricAction,
                        device_id: this.deviceInfo?.device?.deviceId,
                        service_indicator: this.serviceIndicator,
                    },
                    fields: {
                        device_url: this.deviceInfo?.device?.clientDeviceUri,
                        mobius_url: this.deviceInfo?.device?.uri,
                        calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                        reg_source: caller,
                        server_type: serverType,
                        trackingId,
                        error: errorData?.msg,
                        error_type: errorData?.type,
                    },
                    type,
                };
                break;
            }
            case METRIC_EVENT.KEEPALIVE_ERROR: {
                let errorData;
                if (clientError) {
                    errorData = {
                        msg: clientError.getError().message,
                        type: clientError.getError().type,
                    };
                }
                data = {
                    tags: {
                        action: metricAction,
                        device_id: this.deviceInfo?.device?.deviceId,
                        service_indicator: this.serviceIndicator,
                    },
                    fields: {
                        device_url: this.deviceInfo?.device?.clientDeviceUri,
                        mobius_url: this.deviceInfo?.device?.uri,
                        calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                        reg_source: caller,
                        server_type: serverType,
                        trackingId,
                        keepalive_count: keepaliveCount,
                        error: errorData?.msg,
                        error_type: errorData?.type,
                    },
                    type,
                };
                break;
            }
            default:
                log.warn('Invalid metric name received. Rejecting request to submit metric.', {
                    file: METRIC_FILE,
                    method: 'submitRegistrationMetric',
                });
                break;
        }
        if (data) {
            this.webex.internal.metrics.submitClientMetrics(name, data);
        }
    }
    submitCallMetric(name, metricAction, type, callId, correlationId, callError) {
        let data;
        switch (name) {
            case METRIC_EVENT.CALL: {
                data = {
                    tags: {
                        action: metricAction,
                        device_id: this.deviceInfo?.device?.deviceId,
                        service_indicator: this.serviceIndicator,
                    },
                    fields: {
                        device_url: this.deviceInfo?.device?.clientDeviceUri,
                        mobius_url: this.deviceInfo?.device?.uri,
                        calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                        call_id: callId,
                        correlation_id: correlationId,
                    },
                    type,
                };
                break;
            }
            case METRIC_EVENT.CALL_ERROR: {
                if (callError) {
                    data = {
                        tags: {
                            action: metricAction,
                            device_id: this.deviceInfo?.device?.deviceId,
                            service_indicator: this.serviceIndicator,
                        },
                        fields: {
                            device_url: this.deviceInfo?.device?.clientDeviceUri,
                            mobius_url: this.deviceInfo?.device?.uri,
                            calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                            call_id: callId,
                            correlation_id: correlationId,
                            error: callError.getCallError().message,
                            error_type: callError.getCallError().type,
                        },
                        type,
                    };
                }
                break;
            }
            default:
                log.warn('Invalid metric name received. Rejecting request to submit metric.', {
                    file: METRIC_FILE,
                    method: 'submitCallMetric',
                });
                break;
        }
        if (data) {
            this.webex.internal.metrics.submitClientMetrics(name, data);
        }
    }
    submitMediaMetric(name, metricAction, type, callId, correlationId, localSdp, remoteSdp, state, callError) {
        let data;
        switch (name) {
            case METRIC_EVENT.MEDIA: {
                data = {
                    tags: {
                        action: metricAction,
                        device_id: this.deviceInfo?.device?.deviceId,
                        service_indicator: this.serviceIndicator,
                    },
                    fields: {
                        device_url: this.deviceInfo?.device?.clientDeviceUri,
                        mobius_url: this.deviceInfo?.device?.uri,
                        calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                        call_id: callId,
                        correlation_id: correlationId,
                        local_media_details: localSdp,
                        remote_media_details: remoteSdp,
                        state,
                    },
                    type,
                };
                break;
            }
            case METRIC_EVENT.MEDIA_ERROR: {
                if (callError) {
                    data = {
                        tags: {
                            action: metricAction,
                            device_id: this.deviceInfo?.device?.deviceId,
                            service_indicator: this.serviceIndicator,
                        },
                        fields: {
                            device_url: this.deviceInfo?.device?.clientDeviceUri,
                            mobius_url: this.deviceInfo?.device?.uri,
                            calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                            call_id: callId,
                            correlation_id: correlationId,
                            local_media_details: localSdp,
                            remote_media_details: remoteSdp,
                            error: callError.getCallError().message,
                            error_type: callError.getCallError().type,
                        },
                        type,
                    };
                }
                break;
            }
            default:
                log.warn('Invalid metric name received. Rejecting request to submit metric.', {
                    file: METRIC_FILE,
                    method: 'submitMediaMetric',
                });
                break;
        }
        if (data) {
            this.webex.internal.metrics.submitClientMetrics(name, data);
        }
    }
    submitVoicemailMetric(name, metricAction, type, messageId, voicemailError, statusCode) {
        let data;
        switch (name) {
            case METRIC_EVENT.VOICEMAIL: {
                data = {
                    tags: {
                        action: metricAction,
                        device_id: this.deviceInfo?.device?.deviceId,
                        message_id: messageId,
                    },
                    fields: {
                        device_url: this.deviceInfo?.device?.clientDeviceUri,
                        calling_sdk_version: typeof process !== 'undefined' && process.env.CALLING_SDK_VERSION
                            ? process.env.CALLING_SDK_VERSION
                            : VERSION,
                    },
                    type,
                };
                break;
            }
            case METRIC_EVENT.VOICEMAIL_ERROR: {
                data = {
                    tags: {
                        action: metricAction,
                        device_id: this.deviceInfo?.device?.deviceId,
                        message_id: messageId,
                        error: voicemailError,
                        status_code: statusCode,
                    },
                    fields: {
                        device_url: this.deviceInfo?.device?.clientDeviceUri,
                        calling_sdk_version: typeof process !== 'undefined' && process.env.CALLING_SDK_VERSION
                            ? process.env.CALLING_SDK_VERSION
                            : VERSION,
                    },
                    type,
                };
                break;
            }
            default:
                log.warn('Invalid metric name received. Rejecting request to submit metric.', {
                    file: METRIC_FILE,
                    method: 'submitVoicemailMetric',
                });
                break;
        }
        if (data) {
            this.webex.internal.metrics.submitClientMetrics(name, data);
        }
    }
    submitBNRMetric(name, type, callId, correlationId) {
        let data;
        if (name === METRIC_EVENT.BNR_ENABLED || name === METRIC_EVENT.BNR_DISABLED) {
            data = {
                tags: {
                    device_id: this.deviceInfo?.device?.deviceId,
                    service_indicator: this.serviceIndicator,
                },
                fields: {
                    device_url: this.deviceInfo?.device?.clientDeviceUri,
                    mobius_url: this.deviceInfo?.device?.uri,
                    calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
                    call_id: callId,
                    correlation_id: correlationId,
                },
                type,
            };
        }
        else {
            log.warn('Invalid metric name received. Rejecting request to submit metric.', {
                file: METRIC_FILE,
                method: 'submitBNRMetric',
            });
        }
        if (data) {
            this.webex.internal.metrics.submitClientMetrics(name, data);
        }
    }
    submitMobiusSocketMetric(name, metricAction, type, wssUrl, trackingId, error, eventType) {
        let data;
        const commonTags = {
            action: metricAction,
            device_id: this.deviceInfo?.device?.deviceId,
            service_indicator: this.serviceIndicator,
        };
        const commonFields = {
            device_url: this.deviceInfo?.device?.clientDeviceUri,
            mobius_url: this.deviceInfo?.device?.uri,
            calling_sdk_version: process.env.CALLING_SDK_VERSION || VERSION,
            wss_url: wssUrl,
            tracking_id: trackingId,
            event_type: eventType,
        };
        switch (name) {
            case METRIC_EVENT.MOBIUS_SOCKET: {
                data = {
                    tags: commonTags,
                    fields: commonFields,
                    type,
                };
                break;
            }
            case METRIC_EVENT.MOBIUS_SOCKET_ERROR: {
                data = {
                    tags: commonTags,
                    fields: {
                        ...commonFields,
                        error,
                    },
                    type,
                };
                break;
            }
            default:
                log.warn('Invalid metric name received. Rejecting request to submit metric.', {
                    file: METRIC_FILE,
                    method: 'submitMobiusSocketMetric',
                });
                break;
        }
        if (data) {
            this.webex.internal.metrics.submitClientMetrics(name, data);
        }
    }
}
export const getMetricManager = (webex, indicator) => {
    if (!metricManager && webex) {
        metricManager = new MetricManager(webex, indicator);
    }
    return metricManager;
};
export default getMetricManager;
