/**
 * Event builder utilities for creating telemetry events
 *
 * Provides builder functions that auto-populate common fields and ensure
 * schema compliance for all event types.
 */
import { generateEventId, getCurrentTimestamp, getReleaseId, getOsInfo, getPlatformVersions, getMemoryUsage, getCpuTime, isCi, getCiPlatform, } from "./utils";
import { SDK_VERSION } from "../../sdk-version";
// Create base event with auto-populated common fields
// Kept for backward compatibility but can be replaced with direct construction
export function createBaseEvent(context = {}) {
    return {
        event_id: generateEventId(),
        timestamp_ms: getCurrentTimestamp(),
        release_id: getReleaseId(),
        customuser_id: context.customuser_id,
        account_id: context.account_id,
        identity_id: context.identity_id,
        visitor_id: context.visitor_id,
        correlation_id: context.correlation_id,
    };
}
export function buildErrorEvent(data, context = {}) {
    return {
        ...createBaseEvent(context),
        zap_id: context.zap_id,
        node_id: context.node_id,
        selected_api: context.selected_api,
        app_id: context.app_id,
        app_version_id: context.app_version_id,
        environment: context.environment,
        ...data,
        sdk_version: SDK_VERSION,
    };
}
export function buildApplicationLifecycleEvent(data, context = {}) {
    const osInfo = getOsInfo();
    const platformVersions = getPlatformVersions();
    return {
        ...createBaseEvent(context),
        selected_api: context.selected_api,
        app_id: context.app_id,
        app_version_id: context.app_version_id,
        cli_version: null,
        memory_usage_bytes: data.memory_usage_bytes ?? getMemoryUsage(),
        cpu_time_ms: data.cpu_time_ms ?? getCpuTime(),
        os_platform: osInfo.platform,
        os_release: osInfo.release,
        os_architecture: osInfo.architecture,
        platform_versions: platformVersions,
        environment: context.environment ?? (globalThis.process?.env?.NODE_ENV || null),
        is_ci_environment: isCi(),
        ci_platform: getCiPlatform(),
        session_id: null,
        metadata: null,
        process_argv: globalThis.process?.argv || null,
        ...data,
        sdk_version: SDK_VERSION,
    };
}
export function buildErrorEventWithContext(data, context = {}) {
    const executionTime = data.execution_start_time
        ? Date.now() - data.execution_start_time
        : null;
    return {
        ...createBaseEvent(context),
        zap_id: context.zap_id,
        node_id: context.node_id,
        selected_api: context.selected_api,
        app_id: context.app_id,
        app_version_id: context.app_version_id,
        environment: context.environment ?? (globalThis.process?.env?.NODE_ENV || null),
        execution_time_before_error_ms: executionTime,
        ...data,
        sdk_version: SDK_VERSION,
    };
}
export function buildMethodCalledEvent(data, context = {}) {
    return {
        ...createBaseEvent(context),
        method_name: data.method_name ?? null,
        method_module: data.method_module ?? null,
        execution_duration_ms: data.execution_duration_ms,
        success_flag: data.success_flag,
        error_message: data.error_message ?? null,
        error_type: data.error_type ?? null,
        argument_count: data.argument_count,
        is_paginated: data.is_paginated ?? false,
        environment: context.environment ?? (process?.env?.NODE_ENV || null),
        selected_api: data.selected_api ?? context.selected_api ?? null,
        app_id: context.app_id ?? null,
        app_version_id: context.app_version_id ?? null,
        zap_id: context.zap_id ?? null,
        node_id: context.node_id ?? null,
        operation_type: data.operation_type ?? null,
        operation_key: data.operation_key ?? null,
        call_context: null,
        is_retry: false,
        retry_attempt: null,
        argument_types: null,
        return_type: null,
        caller_method: null,
        call_stack_depth: null,
        is_synchronous: false,
        cpu_time_ms: null,
        memory_usage_bytes: null,
        sdk_version: SDK_VERSION,
    };
}
