/**
 * @license
 * Copyright 2025 ReverseCraft
 * SPDX-License-Identifier: Apache-2.0
 */
import{isUtf8 as t}from"node:buffer";import{getConfig as e}from"../utils/config.js";export function truncateUrl(t,e){if(t.length<=e)return t;try{const r=new URL(t),n=r.origin,o=r.pathname+r.search;if(n.length>=e-10)return t.substring(0,e-3)+"...";const s=e-n.length-3;if(s<=0)return n+"...";return n+o.substring(0,s)+"..."}catch{return t.substring(0,e-3)+"..."}}export function getShortDescriptionForRequest(t,r,n=!1){const o=e(),s=truncateUrl(t.url(),o.maxUrlLength);return`reqid=${r} ${t.method()} ${s} ${getStatusFromRequest(t)}${n?" [selected in the DevTools Network panel]":""}`}export function getStatusFromRequest(t){const e=t.response(),r=t.failure();let n;if(e){const t=e.status();n=t>=200&&t<=299?`[success - ${t}]`:`[failed - ${t}]`}else n=r?`[failed - ${r.errorText}]`:"[pending]";return n}export function getFormattedHeaderValue(t){const e=[];for(const[r,n]of Object.entries(t))e.push(`- ${r}:${n}`);return e}export async function getFormattedResponseBody(n,o=e().maxBodySize){try{const e=await n.buffer();if(t(e)){const t=e.toString("utf-8");return 0===t.length?"<empty response>":`${r(t,o)}`}return"<binary data>"}catch{return"<not available anymore>"}}export async function getFormattedRequestBody(t,n=e().maxBodySize){if(t.hasPostData()){const e=t.postData();if(e)return`${r(e,n)}`;try{const e=await t.fetchPostData();if(e)return`${r(e,n)}`}catch{return"<not available anymore>"}}}function r(t,e){return t.length>e?""+(t.substring(0,e)+"... <truncated>"):`${t}`}export function formatUrlForDetail(t,r=100){const n=[];try{const e=new URL(t),o=e.origin+e.pathname;if(n.push(o),e.search){const t=e.searchParams,o=Array.from(t.entries());if(o.length>0){n.push("Query Parameters:");for(const[t,e]of o){const o=e.length>r?e.substring(0,r)+"...<truncated>":e;n.push(`  ${t}: ${o}`)}}}}catch{const r=e();n.push(truncateUrl(t,2*r.maxUrlLength))}return n}