/* eslint-disable @typescript-eslint/no-explicit-any */

import React from "react";

export const calculateLoadTimes = () => {
  // Check performance support
  if (performance === undefined) {
    return [];
  }

  // Get a list of "resource" performance entries
  const resources = performance.getEntriesByType("resource");
  if (resources === undefined || resources.length <= 0) {
    return [];
  }

  const timings = [];
  for (const resource of resources) {
    timings.push({
      name: resource.name,
      initiatorType: resource.initiatorType,
      redirectTime: resource.redirectEnd - resource.redirectStart,
      dnsTime: resource.domainLookupEnd - resource.domainLookupStart,
      tcpHandshakeTime: resource.connectEnd - resource.connectStart,
      secureConnectionTime:
        resource.secureConnectionStart > 0
          ? resource.connectEnd - resource.secureConnectionStart
          : 0,
      responseTime: resource.responseEnd - resource.responseStart,
      fetchUntilResponseEndTime:
        resource.fetchStart > 0
          ? resource.responseEnd - resource.fetchStart
          : 0,
      requestStartUntilResponseEnd:
        resource.requestStart > 0
          ? resource.responseEnd - resource.requestStart
          : 0,
      startUntilResponseEnd:
        resource.startTime > 0 ? resource.responseEnd - resource.startTime : 0,
    });
  }
  return timings;
};
