   requestId: request[kInspectorRequestId],
    timestamp: getMonotonicTime(),
    // TODO(legendecas): distinguish between `undici.request` and `undici.fetch`.
    type: kResourceType.Fetch,
    response: {
      url,
      status: response.statusCode,
      statusText: response.statusText,
      headers: responseHeadersArrayToDictionary(response.headers),
    },
  });
}

/**
 * When a response is completed, emit Network.loadingFinished event.
 * https://chromedevtools.github.io/devtools-protocol/1-3/Network/#event-loadingFinished
 * @param {{ request: undici.Request, response: undici.Response }} event
 */
function onClientResponseFinish({ request }) {
  if (typeof request[kInspectorRequestId] !== 'string') {
    return;
  }
  Network.loadingFinished({
    requestId: request[kInspectorRequestId],
    timestamp: getMonotonicTime(),
  });
}

function enable() {
  dc.subscribe('undici:request:create', onClientRequestStart);
  dc.subscribe('undici:request:error', onClientRequestError);
  dc.subscribe('undici:request:headers', onClientResponseHeaders);
  dc.subscribe('undici:request:trailers', onClientResponseFinish);
}

function disable() {
  dc.unsubscribe('undici:request:create', onClientRequestStart);
  dc.unsubscribe('undici:request:error', onClientRequestError);
  dc.unsubscribe('undici:request:headers', onClientResponseHeaders);
  dc.unsubscribe('undici:request:trailers', onClientResponseFinish);
}

module.exports = {
  enable,
  disable,
};
