import{j as e,c as k,r as p,n as J,o as H,C as G,X,M as K,q as L,y as V}from"./index-CUDottTv.js";import{T as _}from"./topbar-892TvyNb.js";import{B as Y}from"./badge-BOfEUuSW.js";import"./textarea-DAwdLdON.js";import{D as Q}from"./data-table-A2Iq_hk5.js";import{D as Z}from"./detail-panel-YWSVnqP_.js";import{F as ee}from"./filter-bar-CP39inkf.js";import{S as te}from"./status-dot-C1nxHZIG.js";import{T as se}from"./tabs-BoJRkfaU.js";import{D as A}from"./download-CwvkYJyJ.js";import{E as re}from"./export-button-D7y2ugNf.js";import{u as ne}from"./use-connected-3Ljk-Uwv.js";import{f as R,g as ae,a as le}from"./format-CF_gowqw.js";function T(t){return t>=1e3?`${(t/1e3).toFixed(2)}s`:`${Math.round(t)}ms`}function oe({segments:t,total:r,className:a}){return e.jsxs("div",{className:k("space-y-2",a),children:[e.jsx("div",{className:"h-5 flex rounded-md overflow-hidden bg-bg-elevated",children:t.map(s=>{const n=r>0?s.value/r*100:0;return n<=0?null:e.jsx("div",{style:{width:`${n}%`,backgroundColor:s.color},className:"h-full relative group",title:`${s.label}: ${T(s.value)}`,children:n>15&&e.jsx("span",{className:"absolute inset-0 flex items-center justify-center text-[10px] font-medium text-white/80",children:T(s.value)})},s.label)})}),e.jsx("div",{className:"flex items-center gap-4",children:t.map(s=>e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx("div",{className:"w-2 h-2 rounded-full",style:{backgroundColor:s.color}}),e.jsx("span",{className:"text-[11px] text-text-tertiary",children:s.label}),e.jsx("span",{className:"text-[11px] text-text-secondary tabular-nums",children:T(s.value)})]},s.label))})]})}const ie=[["PK","ZIP/Office file","xlsx"],["%PDF","PDF document","pdf"],["PNG","PNG image","png"],["ÿØÿ","JPEG image","jpg"],["GIF8","GIF image","gif"],["RIFF","RIFF file","webp"]];function ce(t){for(const[s,n,l]of ie)if(t.startsWith(s))return{isBinary:!0,label:n,ext:l};const r=t.slice(0,200);let a=0;for(let s=0;s<r.length;s++){const n=r.charCodeAt(s);n<32&&n!==9&&n!==10&&n!==13&&a++}return r.length>0&&a/r.length>.1?{isBinary:!0,label:"Binary data",ext:"bin"}:{isBinary:!1}}function de(t){const r=t.trimStart();if(r.startsWith("{")||r.startsWith("["))try{return JSON.parse(t),"json"}catch{}return/^\w+:["\[{$DIN]/.test(r)||r.startsWith("0:")||/\n\w+:/.test(t.slice(0,200))?"rsc":r.startsWith("<!")||r.startsWith("<html")||r.startsWith("<div")?"html":"text"}function xe(t){const r=t.split(`
`).filter(Boolean),a=[],s=[];for(const l of r){const x=l.indexOf(":");if(x<0||x>4){s.push(l);continue}const i=l.slice(0,x),o=l.slice(x+1);if(o.startsWith("I[")){const c=o.match(/"([^"]+\.(?:tsx?|jsx?))"/),h=o.match(/,"(\w+)"\]$/);if(c){const f=c[1].split("/").pop()??c[1],y=(h==null?void 0:h[1])??"default";a.push(`  ${i}: ${y} from ${f}`)}continue}if(o.startsWith('["$"'))try{const c=JSON.parse(o);if(Array.isArray(c)&&c[0]==="$"&&typeof c[1]=="string"){s.push(`  ${i}: <${c[1]}> element`);continue}}catch{}if(o.startsWith("{")&&o.includes('"name"'))try{const c=JSON.parse(o);if(c.name){const h=c.env?` [${c.env}]`:"",f=c.props&&typeof c.props=="object"?` props={${Object.keys(c.props).join(", ")}}`:"";s.push(`  ${i}: <${c.name}${h}>${f}`);continue}}catch{}if(!(o.startsWith("D{")&&o.includes('"time"'))){if(o.startsWith("{")||o.startsWith("["))try{const c=JSON.parse(o);s.push(`  ${i}: ${JSON.stringify(c,null,2).split(`
`).join(`
    `)}`);continue}catch{}o.length>0&&o!=="null"&&s.push(`  ${i}: ${o.slice(0,200)}${o.length>200?"...":""}`)}}const n=[];return a.length>0&&n.push(`Components (${a.length}):
${a.join(`
`)}`),s.length>0&&n.push(`Data:
${s.join(`
`)}`),`React Server Components Payload
${"─".repeat(40)}

${n.join(`

`)}`}function ue(t){try{return JSON.parse(t)}catch{return null}}function B(t){return JSON.stringify(t,null,2)}function F(t,r="root"){function a(s,n,l){if(s==null)return`${l}<${n}/>
`;if(typeof s=="object"&&Array.isArray(s))return s.map((i,o)=>a(i,"item",l)).join("");if(typeof s=="object"&&s!==null){const i=Object.entries(s).map(([o,c])=>a(c,o,l+"  ")).join("");return`${l}<${n}>
${i}${l}</${n}>
`}const x=String(s).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");return`${l}<${n}>${x}</${n}>
`}return`<?xml version="1.0" encoding="UTF-8"?>
${a(t,r,"")}`}function M(t){const r=Array.isArray(t)?t:[t];if(r.length===0)return"";const a=r[0];if(typeof a!="object"||a===null)return JSON.stringify(r);const s=Object.keys(a),n=i=>{const o=i==null?"":typeof i=="object"?JSON.stringify(i):String(i);return o.includes(",")||o.includes('"')||o.includes(`
`)?`"${o.replace(/"/g,'""')}"`:o},l=s.join(","),x=r.map(i=>s.map(o=>n(i[o])).join(","));return[l,...x].join(`
`)}function S(t,r=0){const a="  ".repeat(r);if(t==null)return"null";if(typeof t=="boolean"||typeof t=="number")return String(t);if(typeof t=="string")return t.includes(`
`)||t.includes(":")||t.includes("#")?`"${t.replace(/"/g,'\\"')}"`:t;if(Array.isArray(t))return t.length===0?"[]":t.map(s=>{const n=S(s,r+1);return typeof s=="object"&&s!==null?`${a}- ${n.trimStart()}`:`${a}- ${n}`}).join(`
`);if(typeof t=="object"){const s=Object.entries(t);return s.length===0?"{}":s.map(([n,l])=>{const x=S(l,r+1);return typeof l=="object"&&l!==null&&(Array.isArray(l)?l.length>0:Object.keys(l).length>0)?`${a}${n}:
${x}`:`${a}${n}: ${x}`}).join(`
`)}return String(t)}function I(t,r,a){const s=new Blob([t],{type:a}),n=URL.createObjectURL(s),l=document.createElement("a");l.href=n,l.download=r,l.click(),URL.revokeObjectURL(n)}function P(t,r){try{const a=atob(t),s=new Uint8Array(a.length);for(let i=0;i<a.length;i++)s[i]=a.charCodeAt(i);const n=new Blob([s]),l=URL.createObjectURL(n),x=document.createElement("a");x.href=l,x.download=r,x.click(),URL.revokeObjectURL(l)}catch{I(t,r,"application/octet-stream")}}function U({content:t,label:r="Response",filename:a="response"}){var O;const[s,n]=p.useState("pretty"),[l,x]=p.useState(!1),[i,o]=p.useState(!1),[c,h]=p.useState(!1);p.useEffect(()=>{if(!c)return;const b=v=>{v.key==="Escape"&&h(!1)};return window.addEventListener("keydown",b),()=>window.removeEventListener("keydown",b)},[c]);const f=p.useMemo(()=>ce(t),[t]),y=p.useMemo(()=>de(t),[t]),d=p.useMemo(()=>y==="json"?ue(t):null,[t,y]),g=d!==null,C=y==="rsc",u=p.useMemo(()=>{if(C)return s==="raw"?t:xe(t);if(!g)return t;switch(s){case"pretty":return B(d);case"raw":return t;case"xml":return F(d);case"csv":return M(d);case"yaml":return S(d)}},[t,d,g,s]),m=p.useCallback(()=>{navigator.clipboard.writeText(u),x(!0),setTimeout(()=>x(!1),2e3)},[u]),j=p.useCallback(b=>{if(o(!1),f.isBinary){P(t,`${a}.${f.ext??"bin"}`);return}const v={json:"application/json",xml:"application/xml",csv:"text/csv",yaml:"text/yaml",raw:"text/plain"},z={json:"json",xml:"xml",csv:"csv",yaml:"yaml",raw:"txt"},w=b==="pretty"?"json":b,W=w==="json"?B(d):w==="xml"?F(d):w==="csv"?M(d):w==="yaml"?S(d):t;I(W,`${a}.${z[w]??"txt"}`,v[w]??"text/plain")},[t,d,f,a]);if(f.isBinary)return e.jsxs("div",{className:"rounded-md border border-border-default bg-bg-input p-6 text-center space-y-3",children:[e.jsx("div",{className:"text-text-muted text-sm",children:f.label}),e.jsx("div",{className:"text-text-tertiary text-xs",children:t.length>1024?`${(t.length/1024).toFixed(1)} KB`:`${t.length} bytes`}),e.jsxs("button",{onClick:()=>P(t,`${a}.${f.ext??"bin"}`),className:"inline-flex items-center gap-2 px-3 py-1.5 rounded-md bg-accent text-white text-sm font-medium hover:bg-accent/90 transition-colors cursor-pointer",children:[e.jsx(A,{size:14}),"Download ",((O=f.ext)==null?void 0:O.toUpperCase())??"file"]})]});const q={pretty:"Pretty",raw:"Raw",xml:"XML",csv:"CSV",yaml:"YAML"},D=e.jsxs("div",{className:"flex items-center justify-between px-3 py-1.5 border-b border-border-muted shrink-0",children:[e.jsx("div",{className:"flex items-center gap-1",children:g?Object.entries(q).map(([b,v])=>e.jsx("button",{onClick:()=>n(b),className:k("px-2 py-0.5 rounded text-[10px] font-medium uppercase tracking-wider transition-colors cursor-pointer",s===b?"bg-accent/10 text-accent":"text-text-muted hover:text-text-secondary"),children:v},b)):C?e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:()=>n("pretty"),className:k("px-2 py-0.5 rounded text-[10px] font-medium uppercase tracking-wider transition-colors cursor-pointer",s==="pretty"?"bg-accent/10 text-accent":"text-text-muted hover:text-text-secondary"),children:"Parsed"}),e.jsx("button",{onClick:()=>n("raw"),className:k("px-2 py-0.5 rounded text-[10px] font-medium uppercase tracking-wider transition-colors cursor-pointer",s==="raw"?"bg-accent/10 text-accent":"text-text-muted hover:text-text-secondary"),children:"Raw"}),e.jsx("span",{className:"ml-1 px-1.5 py-0.5 rounded bg-purple/10 text-purple text-[9px] font-medium uppercase",children:"RSC"})]}):e.jsx("span",{className:"text-[10px] font-medium text-text-muted uppercase tracking-wider",children:y==="html"?"HTML":r})}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("button",{onClick:m,className:"p-1 rounded text-text-muted hover:text-text-secondary hover:bg-bg-hover transition-all cursor-pointer",title:"Copy",children:l?e.jsx(J,{size:13}):e.jsx(H,{size:13})}),e.jsxs("div",{className:"relative",children:[e.jsxs("button",{onClick:()=>o(!i),className:"p-1 rounded text-text-muted hover:text-text-secondary hover:bg-bg-hover transition-all cursor-pointer flex items-center gap-0.5",title:"Download",children:[e.jsx(A,{size:13}),e.jsx(G,{size:10})]}),i&&e.jsx("div",{className:"absolute right-0 top-full mt-1 bg-bg-elevated border border-border-default rounded-md shadow-lg z-50 py-1 min-w-[120px]",children:g?e.jsxs(e.Fragment,{children:[e.jsx(N,{onClick:()=>j("json"),label:"JSON"}),e.jsx(N,{onClick:()=>j("xml"),label:"XML"}),e.jsx(N,{onClick:()=>j("csv"),label:"CSV"}),e.jsx(N,{onClick:()=>j("yaml"),label:"YAML"})]}):e.jsx(N,{onClick:()=>j("raw"),label:"Download"})})]}),e.jsx("button",{onClick:()=>h(!c),className:"p-1 rounded text-text-muted hover:text-text-secondary hover:bg-bg-hover transition-all cursor-pointer",title:c?"Exit fullscreen":"Fullscreen",children:c?e.jsx(X,{size:13}):e.jsx(K,{size:13})})]})]}),E=e.jsx("pre",{className:"p-4 overflow-auto flex-1 min-h-0",children:e.jsx("code",{className:"font-mono text-[12px] leading-relaxed text-text-secondary whitespace-pre-wrap break-words",children:u})});return c?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"rounded-md border border-border-default bg-bg-input p-4 text-center",children:e.jsx("button",{onClick:()=>h(!1),className:"text-sm text-accent hover:underline cursor-pointer",children:"Viewing fullscreen — click to close"})}),e.jsx("div",{className:"fixed inset-0 z-[100] bg-bg-default/95 backdrop-blur-sm flex items-center justify-center p-8",onClick:()=>h(!1),children:e.jsxs("div",{className:"bg-bg-surface border border-border-default rounded-lg shadow-2xl flex flex-col w-full max-w-5xl",style:{height:"calc(100vh - 100px)"},onClick:b=>b.stopPropagation(),children:[D,E]})})]}):e.jsxs("div",{className:"flex flex-col rounded-md border border-border-default bg-bg-input flex-1 min-h-0",children:[D,E]})}function N({onClick:t,label:r}){return e.jsx("button",{onClick:t,className:"block w-full text-left px-3 py-1.5 text-[12px] text-text-secondary hover:bg-bg-hover transition-colors cursor-pointer",children:r})}function pe({itemCount:t,onSelect:r,onDeselect:a,enabled:s=!0}){const[n,l]=p.useState(0),x=p.useCallback(i=>{if(!s||t===0)return;const o=i.target;if(!(o.tagName==="INPUT"||o.tagName==="TEXTAREA"||o.isContentEditable))switch(i.key){case"j":case"ArrowDown":i.preventDefault(),l(c=>Math.min(c+1,t-1));break;case"k":case"ArrowUp":i.preventDefault(),l(c=>Math.max(c-1,0));break;case"Enter":i.preventDefault(),r==null||r(n);break;case"Escape":i.preventDefault(),a==null||a();break}},[s,t,n,r,a]);return p.useEffect(()=>(window.addEventListener("keydown",x),()=>window.removeEventListener("keydown",x)),[x]),p.useEffect(()=>{n>=t&&l(Math.max(0,t-1))},[t,n]),{selectedIndex:n,setSelectedIndex:l}}const me={green:"text-green",blue:"text-blue",amber:"text-amber",red:"text-red"},he={GET:"blue",POST:"green",PUT:"amber",PATCH:"purple",DELETE:"red"};function $(t){if(t.graphqlOperation)return t.graphqlOperation.name;try{const r=new URL(t.url).pathname,a=r.split("/").filter(Boolean);return a[a.length-1]||r}catch{return t.url}}const fe=[{key:"name",header:"Name",width:"180px",render:t=>e.jsx("span",{className:"font-medium truncate block max-w-[160px]",children:$(t)})},{key:"status",header:"Status",width:"80px",render:t=>{const r=ae(t.status);return e.jsxs("span",{className:"inline-flex items-center gap-1.5",children:[e.jsx(te,{color:r,size:"sm"}),e.jsx("span",{className:me[r],children:t.status})]})}},{key:"method",header:"Method",width:"90px",render:t=>{const r=t.method,a=he[r]??"default";return e.jsx(Y,{variant:a,size:"sm",children:r})}},{key:"url",header:"URL",render:t=>e.jsx("span",{className:"text-text-secondary truncate block max-w-[300px] font-mono text-xs",children:t.url})},{key:"duration",header:"Duration",width:"100px",render:t=>e.jsx("span",{className:"tabular-nums",children:R(t.duration)})},{key:"size",header:"Size",width:"80px",render:t=>e.jsx("span",{className:"tabular-nums text-text-tertiary",children:le(t.responseBodySize)})}];function De(){const[t,r]=p.useState("all"),[a,s]=p.useState(null),[n,l]=p.useState("headers"),[x,i]=p.useState("");ne();const o=L(u=>u.network),c=L(u=>u.initialLoadDone),h=o,f=p.useMemo(()=>{let u=h;if(t==="fetch"&&(u=u.filter(m=>m.source==="fetch"&&!m.graphqlOperation)),t==="xhr"&&(u=u.filter(m=>m.source==="xhr")),t==="graphql"&&(u=u.filter(m=>m.graphqlOperation)),x){const m=x.toLowerCase();u=u.filter(j=>j.url.toLowerCase().includes(m)||$(j).toLowerCase().includes(m))}return u},[t,x,h]),{selectedIndex:y}=pe({itemCount:f.length,onSelect:u=>s(u),onDeselect:()=>s(null)}),d=a!==null?f[a]:null,g=p.useCallback((u,m)=>s(m),[]),C=p.useCallback(()=>s(null),[]);return e.jsxs("div",{className:"flex-1 flex flex-col min-h-0 overflow-hidden",children:[e.jsx(_,{tabs:[{id:"all",label:"All"},{id:"fetch",label:"Fetch"},{id:"xhr",label:"XHR"},{id:"graphql",label:"GraphQL"}],activeTab:t,onTabChange:r}),e.jsxs("div",{className:"flex-1 flex min-h-0 overflow-hidden",children:[e.jsxs("div",{className:"flex-1 flex flex-col min-h-0 overflow-hidden",children:[e.jsx(ee,{search:x,onSearchChange:i,searchPlaceholder:"Filter by URL or name...",children:e.jsx(re,{data:f,filename:"network-events"})}),e.jsx("div",{className:"flex-1 overflow-auto",children:!c&&h.length===0?e.jsx(V,{rows:10}):e.jsx(Q,{columns:fe,data:f,selectedIndex:a??y,onRowClick:g})})]}),e.jsx(Z,{open:d!==null,onClose:C,title:d?$(d):"",subtitle:d?`${d.method} ${d.status} · ${R(d.duration)}`:"",children:d&&e.jsxs("div",{className:"flex flex-col flex-1 min-h-0",children:[e.jsx(se,{tabs:[{id:"headers",label:"Headers"},{id:"request",label:"Request"},{id:"response",label:"Response"},{id:"timing",label:"Timing"}],activeTab:n,onTabChange:l}),e.jsxs("div",{className:k("p-4 flex-1 min-h-0",n==="request"||n==="response"?"flex flex-col":""),children:[n==="headers"&&e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("h4",{className:"text-[11px] font-medium text-text-muted uppercase tracking-wider mb-2",children:"Request Headers"}),e.jsx("div",{className:"space-y-1",children:Object.entries(d.requestHeaders).map(([u,m])=>e.jsxs("div",{className:"flex gap-2 text-[13px] font-mono",children:[e.jsxs("span",{className:"text-accent shrink-0",children:[u,":"]}),e.jsx("span",{className:"text-text-secondary truncate",children:m})]},u))})]}),e.jsxs("div",{children:[e.jsx("h4",{className:"text-[11px] font-medium text-text-muted uppercase tracking-wider mb-2",children:"Response Headers"}),e.jsx("div",{className:"space-y-1",children:Object.entries(d.responseHeaders).map(([u,m])=>e.jsxs("div",{className:"flex gap-2 text-[13px] font-mono",children:[e.jsxs("span",{className:"text-accent shrink-0",children:[u,":"]}),e.jsx("span",{className:"text-text-secondary truncate",children:m})]},u))})]})]}),n==="request"&&e.jsx("div",{className:"flex flex-col flex-1 min-h-0",children:d.requestBody?e.jsx(U,{content:d.requestBody,label:"Request",filename:`request-${$(d)}`}):e.jsxs("div",{className:"space-y-2",children:[e.jsx("p",{className:"text-sm text-text-muted",children:"No request body captured"}),e.jsxs("p",{className:"text-xs text-text-tertiary",children:["Enable body capture in your SDK config:",e.jsx("code",{className:"ml-1 px-1.5 py-0.5 bg-bg-elevated rounded text-text-secondary",children:"captureBody: true"})]})]})}),n==="response"&&e.jsx("div",{className:"flex flex-col flex-1 min-h-0",children:d.responseBody?e.jsx(U,{content:d.responseBody,label:"Response",filename:`response-${$(d)}`}):e.jsxs("div",{className:"space-y-2",children:[e.jsx("p",{className:"text-sm text-text-muted",children:"No response body captured"}),e.jsxs("p",{className:"text-xs text-text-tertiary",children:["Enable body capture in your SDK config:",e.jsx("code",{className:"ml-1 px-1.5 py-0.5 bg-bg-elevated rounded text-text-secondary",children:"captureBody: true"})]})]})}),n==="timing"&&e.jsxs("div",{className:"space-y-4",children:[e.jsx(oe,{segments:[{label:"TTFB",value:d.ttfb,color:"var(--color-blue)"},{label:"Download",value:d.duration-d.ttfb,color:"var(--color-green)"}],total:d.duration}),e.jsxs("div",{className:"text-sm text-text-secondary",children:["Total:"," ",e.jsx("span",{className:"text-text-primary font-medium",children:R(d.duration)})]})]})]})]})})]})]})}export{De as NetworkPage};
