import type {HTTPRequest} from "../types/httpRequest.ts";

export default function addTimingToRequest(request: HTTPRequest, timingData: any) {
    if (timingData.dnsStart >= 0 && timingData.dnsEnd >= 0) {
        request.timing.dnsStart = timingData.dnsStart * 1000 // Convert to microseconds
        request.timing.dnsEnd = timingData.dnsEnd * 1000
    }

    // Extract connection timing
    if (timingData.connectStart >= 0 && timingData.connectEnd >= 0) {
        request.timing.connectStart = timingData.connectStart * 1000 // Convert to microseconds
        request.timing.connectEnd = timingData.connectEnd * 1000
    }

    // Extract SSL timing if available
    if (timingData.sslStart >= 0 && timingData.sslEnd >= 0) {
        request.timing.sslStart = timingData.sslStart * 1000 // Convert to microseconds
        request.timing.sslEnd = timingData.sslEnd * 1000
    }

    // Extract send/receive timing
    if (timingData.sendStart >= 0) {
        request.timing.sendStart = timingData.sendStart * 1000
    }
    if (timingData.sendEnd >= 0) {
        request.timing.sendEnd = timingData.sendEnd * 1000
    }
    if (timingData.receiveHeadersStart >= 0) {
        request.timing.receiveHeadersStart = timingData.receiveHeadersStart * 1000
    }
    if (timingData.receiveHeadersEnd >= 0) {
        request.timing.receiveHeadersEnd = timingData.receiveHeadersEnd * 1000
    }
}