1import{r as c,j as e}from"./ui-CPW3-RCR.js";import{g as xe,a as pe,s as T,d as f,y as x,J as p,L as b,a4 as G,Z as U,z as N,E as w,T as W,B as I,C as he,a5 as ue,c as fe,k as je}from"./index-BraPXa8T.js";import{D as C}from"./DepartmentBrainPanel-BXjmvOXs.js";import{A as ye}from"./AIResponseFeedback-UIkBJjrS.js";import{H as ge}from"./hard-drive-CPPAW9W7.js";import{T as be}from"./table-CUdv-10Z.js";import{P as ee}from"./play-D97WNvd1.js";import{M as Ne}from"./index-BfRVBusC.js";import{r as we}from"./index-CbedGEwf.js";import"./supabase-B2z4OhLQ.js";import"./charts-DCB9_QAo.js";import"./vendor-CIXof4MH.js";import"./useSmartScroll-Da1xVHwy.js";import"./scroll-area-BQtXZnuf.js";import"./index-BdQq_4o_.js";import"./useDemoLocale-COy7_g6Y.js";import"./thumbs-up-6rv6gXsR.js";function Pe(){var Z;const{language:O,t:a}=xe(),{selectedClient:k}=pe(),o=(k==null?void 0:k.id)||null,[j,se]=c.useState([]),[y,te]=c.useState(""),[ae,M]=c.useState(!1),[m,B]=c.useState(""),[r,R]=c.useState(null),[L,D]=c.useState(!1),[re,A]=c.useState(null),[l,g]=c.useState([]),[u,q]=c.useState(""),[h,E]=c.useState(!1),v=c.useRef(null);c.useEffect(()=>{var s;(s=v.current)==null||s.scrollTo({top:v.current.scrollHeight,behavior:"smooth"})},[l]);const F=c.useCallback(async()=>{var s,t;if(o){M(!0);try{const n=await T.auth.getSession(),d=await(await fetch("https://ecrgkarlfnflbtpqsmun.supabase.co/functions/v1/bigquery-datasets",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${(s=n.data.session)==null?void 0:s.access_token}`,apikey:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImVjcmdrYXJsZm5mbGJ0cHFzbXVuIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzAzODU2OTIsImV4cCI6MjA4NTk2MTY5Mn0.5mfCnqenWJXyyafphy_dr7-ZP9_SFxJvga_DrKLLSAk"},body:JSON.stringify({client_id:o})})).json();d.error&&!((t=d.datasets)!=null&&t.length)?A(!1):(A(!0),se(d.datasets||[]),te(d.projectId||""))}catch{f.error(a("bq.errorLoading"))}finally{M(!1)}}},[o,a]);c.useEffect(()=>{F()},[F]);const P=c.useCallback(async()=>{var s;if(!(!o||!m.trim())){D(!0),R(null);try{const t=await T.auth.getSession(),i=await(await fetch("https://ecrgkarlfnflbtpqsmun.supabase.co/functions/v1/bigquery-query",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${(s=t.data.session)==null?void 0:s.access_token}`,apikey:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImVjcmdrYXJsZm5mbGJ0cHFzbXVuIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzAzODU2OTIsImV4cCI6MjA4NTk2MTY5Mn0.5mfCnqenWJXyyafphy_dr7-ZP9_SFxJvga_DrKLLSAk"},body:JSON.stringify({client_id:o,query:m.trim()})})).json();i.error?f.error(i.error):(R({schema:i.schema,rows:i.rows,totalRows:i.totalRows,totalBytesProcessed:i.totalBytesProcessed,cacheHit:i.cacheHit}),f.success(a("bq.rowsReturned").replace("{n}",String(i.totalRows))))}catch{f.error(a("bq.queryError"))}finally{D(!1)}}},[o,m,a]),X=c.useCallback(async()=>{var n,i,d,_,$;if(!u.trim()||h||!o)return;const s={role:"user",content:u.trim()},t=[...l,s];g(t),q(""),E(!0);try{const S=await T.auth.getSession(),K=await fetch("https://ecrgkarlfnflbtpqsmun.supabase.co/functions/v1/bigquery-ai-chat",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${(n=S.data.session)==null?void 0:n.access_token}`,apikey:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImVjcmdrYXJsZm5mbGJ0cHFzbXVuIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NzAzODU2OTIsImV4cCI6MjA4NTk2MTY5Mn0.5mfCnqenWJXyyafphy_dr7-ZP9_SFxJvga_DrKLLSAk"},body:JSON.stringify({client_id:o,messages:t,bq_context:{projectId:y,datasets:j.slice(0,5),lastQueryResult:r?{query:m,totalRows:r.totalRows,totalBytesProcessed:r.totalBytesProcessed,schema:r.schema,sampleRows:r.rows.slice(0,5)}:void 0},language:O})});if(!K.ok)throw new Error("AI request failed");const Q=(i=K.body)==null?void 0:i.getReader(),ie=new TextDecoder;let V="";for(g(J=>[...J,{role:"assistant",content:""}]);Q;){const{done:J,value:oe}=await Q.read();if(J)break;const le=ie.decode(oe,{stream:!0});for(const Y of le.split(` 2`)){if(!Y.startsWith("data: "))continue;const H=Y.slice(6);if(H==="[DONE]")break;try{const me=(($=(_=(d=JSON.parse(H).choices)==null?void 0:d[0])==null?void 0:_.delta)==null?void 0:$.content)||"";V+=me,g(de=>{const z=[...de];return z[z.length-1]={role:"assistant",content:V},z})}catch{}}}}catch{g(S=>[...S,{role:"assistant",content:a("bq.aiError")}])}finally{E(!1)}},[u,l,h,o,y,j,r,m,O,a]),ne=s=>{const t=parseInt(s);return t<1024?`${t} B`:t<1024*1024?`${(t/1024).toFixed(1)} KB`:t<1024*1024*1024?`${(t/(1024*1024)).toFixed(1)} MB`:`${(t/(1024*1024*1024)).toFixed(2)} GB`};if(!o)return e.jsxs("div",{className:"space-y-5",children:[e.jsx(C,{sectionKey:"platform-bigquery"}),e.jsx(x,{children:e.jsx(p,{className:"py-8 text-center text-sm text-muted-foreground",children:a("bq.selectClient")}
2)})]});if(ae)return e.jsxs("div",{className:"space-y-5",children:[e.jsx(C,{sectionKey:"platform-bigquery"}),e.jsx(x,{children:e.jsx(p,{className:"py-10 flex justify-center",children:e.jsx(b,{className:"w-5 h-5 animate-spin text-primary"})})})]});if(re===!1)return e.jsxs("div",{className:"space-y-5",children:[e.jsx(C,{sectionKey:"platform-bigquery"}),e.jsx(x,{children:e.jsxs(p,{className:"py-8 text-center space-y-2",children:[e.jsx(G,{className:"w-8 h-8 mx-auto text-muted-foreground"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:a("bq.notConnected")}),e.jsx("p",{className:"text-xs text-muted-foreground",children:a("bq.connectHint")})]})})]});const ce=[a("bq.quickWrite"),a("bq.quickAnalyze"),a("bq.quickSummary"),a("bq.quickGA4")];return e.jsxs("div",{className:"space-y-5",children:[e.jsx(C,{sectionKey:"platform-bigquery"}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(G,{className:"w-4 h-4 text-primary"}),e.jsxs("h2",{className:"text-sm font-semibold",children:["BigQuery â ",y]})]}),e.jsxs(U,{variant:"outline",className:"text-[10px]",children:[j.length," ",a("bq.datasets")]})]}),e.jsx("div",{className:"grid gap-3 md:grid-cols-2 lg:grid-cols-3",children:j.map(s=>{var t;return e.jsxs(x,{className:"group",children:[e.jsxs(N,{className:"pb-1.5 pt-3 px-3",children:[e.jsxs(w,{className:"text-xs flex items-center gap-1.5",children:[e.jsx(ge,{className:"w-3 h-3 text-primary"}),s.id]}),s.location&&e.jsx("span",{className:"text-[10px] text-muted-foreground",children:s.location})]}),e.jsx(p,{className:"px-3 pb-3",children:(t=s.tables)!=null&&t.length?e.jsxs("div",{className:"space-y-1",children:[s.tables.slice(0,8).map(n=>e.jsxs("button",{className:"flex items-center gap-1.5 w-full text-left text-[10px] p-1 rounded hover:bg-muted/60 transition-colors",onClick:()=>B(`SELECT * FROM \`${y}.${s.id}.${n.id}\` LIMIT 100`),children:[e.jsx(be,{className:"w-2.5 h-2.5 text-muted-foreground shrink-0"}),e.jsx("span",{className:"truncate font-medium",children:n.id}),e.jsxs("span",{className:"text-muted-foreground ms-auto shrink-0",children:[parseInt(n.rowCount).toLocaleString()," ",a("bq.rows")]})]},n.id)),s.tables.length>8&&e.jsxs("p",{className:"text-[10px] text-muted-foreground ps-4",children:["+",s.tables.length-8," more"]})]}):e.jsx("p",{className:"text-[10px] text-muted-foreground",children:a("bq.noTables")})})]},s.id)})}),e.jsxs(x,{children:[e.jsx(N,{className:"pb-2",children:e.jsxs(w,{className:"text-xs flex items-center gap-1.5",children:[e.jsx(ee,{className:"w-3.5 h-3.5 text-primary"}),a("bq.sqlEditor")]})}),e.jsxs(p,{children:[e.jsx(W,{value:m,onChange:s=>B(s.target.value),placeholder:"SELECT * FROM `project.dataset.table` LIMIT 100",className:"font-mono text-xs min-h-[80px] max-h-40 resize-none",onKeyDown:s=>{s.key==="Enter"&&(s.metaKey||s.ctrlKey)&&(s.preventDefault(),P())}}),e.jsxs("div",{className:"flex items-center gap-2 mt-2",children:[e.jsxs(I,{size:"sm",onClick:P,disabled:L||!m.trim(),className:"text-xs h-7 gap-1.5",children:[L?e.jsx(b,{className:"w-3 h-3 animate-spin"}):e.jsx(ee,{className:"w-3 h-3"}),a("bq.run")," ",e.jsx("kbd",{className:"hidden sm:inline text-[9px] opacity-60",children:"ââµ"})]}),r&&e.jsxs("div",{className:"flex items-center gap-2 text-[10px] text-muted-foreground",children:[e.jsxs("span",{children:[r.totalRows.toLocaleString()," ",a("bq.rows")]}),e.jsx("span",{children:"·"}),e.jsx("span",{children:ne(r.totalBytesProcessed)}),r.cacheHit&&e.jsxs(U,{variant:"secondary",className:"text-[9px] h-4 px-1",children:[e.jsx(he,{className:"w-2.5 h-2.5 me-0.5"})," Cache"]})]})]})]})]}),r&&r.rows.length>0&&e.jsxs(x,{children:[e.jsx(N,{className:"pb-2",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(w,{className:"text-xs",children:a("bq.results")}),e.jsxs(I,{variant:"ghost",size:"sm",className:"h-6 text-[10px] gap-1",onClick:()=>{const s=[r.schema.map(t=>t.name).join(","),...r.rows.map(t=>r.schema.map(n=>JSON.stringify(t[n.name]??"")).join(","))].join(` 3`);navigator.clipboard.writeText(s),f.success(a("bq.copiedClipboard"))},children:[e.jsx(ue,{className:"w-2.5 h-2.5"})," CSV"]})]})}),e.jsxs(p,{children:[e.jsx("div",{className:"overflow-x-auto max-h-80",children:e.jsxs("table",{className:"w-full text-[10px]",children:[e.jsx("thead",{children:e.jsx("tr",{className:"border-b",children:r.schema.map(s=>e.jsxs("th",{className:"text-start p-1.5 font-medium text-muted-foreground whitespace-nowrap",children:[s.name,e.jsx("span",{className:"text-[9px] opacity-50 ms-1",children:s.type})]},s.name))})}),e.jsx("tbody",{children:r.rows.slice(0,100).map((s,t)=>e.jsx("tr",{className:"border-b last:border-0 hover:bg-muted/40 transition-colors",children:r.schema.map(n=>e.jsx("td",{className:"p-1.5 tabular-nums whitespace-nowrap max-w-[200px] truncate",children:s[n.name]??"null"},n.name))},t))})]})}),r.rows.length>100&&e.jsx("p",{className:"text-[10px] text-muted-foreground mt-2 text-center",children:a("bq.showing").replace("{n}
3",r.totalRows.toLocaleString())})]})]}),e.jsxs(x,{children:[e.jsx(N,{className:"pb-2",children:e.jsxs(w,{className:"text-xs flex items-center gap-1.5",children:[e.jsx(fe,{className:"w-3.5 h-3.5 text-primary"}),"BigQuery AI Analyst"]})}),e.jsxs(p,{children:[l.length===0&&e.jsx("div",{className:"flex flex-wrap gap-1.5 mb-3",children:ce.map(s=>e.jsx(I,{variant:"outline",size:"sm",className:"text-[10px] h-6 px-2",onClick:()=>q(s),children:s},s))}),e.jsxs("div",{ref:v,className:"max-h-72 overflow-y-auto space-y-2 mb-3",children:[l.map((s,t)=>{var n;return e.jsx("div",{className:`text-xs p-2 rounded ${s.role==="user"?"bg-muted ms-8":"bg-primary/5 me-4"}`,children:s.role==="assistant"?e.jsxs("div",{className:"ai-prose",children:[e.jsx(Ne,{remarkPlugins:[we],children:s.content}),!h&&s.content&&e.jsx("div",{className:"mt-1",children:e.jsx(ye,{userMessage:((n=l[t-1])==null?void 0:n.content)||"",aiResponse:s.content,clientId:o||"",chatSource:"bigquery"})})]}):e.jsx("p",{children:s.content})},t)}),h&&((Z=l[l.length-1])==null?void 0:Z.role)!=="assistant"&&e.jsxs("div",{className:"flex items-center gap-1.5 text-xs text-muted-foreground p-2",children:[e.jsx(b,{className:"w-3 h-3 animate-spin"}),a("bq.analyzing")]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(W,{value:u,onChange:s=>q(s.target.value),placeholder:a("bq.askPlaceholder"),className:"min-h-[36px] max-h-20 text-xs resize-none",onKeyDown:s=>{s.key==="Enter"&&!s.shiftKey&&(s.preventDefault(),X())}}),e.jsx(I,{size:"sm",onClick:X,disabled:h||!u.trim(),className:"shrink-0 h-9 w-9 p-0",children:h?e.jsx(b,{className:"w-3.5 h-3.5 animate-spin"}):e.jsx(je,{className:"w-3.5 h-3.5"})})]})]})]})]})}export{Pe as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.