import{o as e}from"./rolldown-runtime-BM3Ffeng.js";import{Ad as t,Gd as n,Gy as r,Hy as i,Jd as a,Ly as o,Zv as s,dg as c,fg as l,hv as u,jg as d,lh as f,m_ as p,mv as m,rv as h,yf as g}from"./index-DDwwBb9e.js";var _=e(r()),v=i(),y=600;function b({organizationId:e,sourceType:r,statements:i,onChange:b,enabled:x}){let[S,C]=(0,_.useState)([]),[w,T]=(0,_.useState)(!1),E=f.ingestionSources.ottlStarter.useQuery({organizationId:e,sourceType:r},{enabled:x&&!!e&&!!r,refetchOnWindowFocus:!1}).data?.statements,D=f.ingestionSources.validateOttl.useMutation(),O=(0,_.useCallback)(async t=>{if(t.filter(e=>e.trim().length>0).length===0){C(t.map(()=>({ok:!0,message:null})));return}T(!0);try{let n=await D.mutateAsync({organizationId:e,statements:t});if(n.ok)C(t.map(()=>({ok:!0,message:null})));else{let e=new Map;for(let t of n.errors){let n=t.line>0?` (line ${t.line}, col ${t.col})`:``;e.set(t.statementIndex,`${t.message}${n}`)}C(t.map((t,n)=>{let r=e.get(n);return r?{ok:!1,message:r}:{ok:!0,message:null}}))}}catch(e){C(t.map(()=>({ok:!0,message:`Validation unavailable: ${e.message}`})))}finally{T(!1)}},[e,D]);(0,_.useEffect)(()=>{let e=setTimeout(()=>{O(i)},y);return()=>clearTimeout(e)},[i.join(`
`)]);let k=(e,t)=>{let n=i.slice();n[e]=t,b(n)},A=e=>{b(i.filter((t,n)=>n!==e))},j=()=>{b([...i,``])},M=()=>{E&&b([...E])},N=(E??[]).length>0,P=i.length===0,F=(0,_.useMemo)(()=>{if(!E||E.length===0||E.length!==i.length)return!1;for(let e=0;e<E.length;e++)if((E[e]??``)!==(i[e]??``))return!1;return!0},[E,i]);return x?(0,v.jsxs)(c,{align:`stretch`,gap:2,children:[(0,v.jsxs)(p,{alignItems:`end`,children:[(0,v.jsxs)(c,{align:`start`,gap:0,children:[(0,v.jsx)(s,{fontSize:`xs`,fontWeight:`semibold`,color:`fg.muted`,children:`OTTL extraction statements`}),(0,v.jsxs)(s,{fontSize:`xs`,color:`fg.muted`,children:[`Each line maps an upstream OTLP attribute onto the canonical`,` `,(0,v.jsx)(`code`,{children:`langwatch.*`}),` namespace. The aigateway evaluates them in order via embedded `,(0,v.jsx)(`code`,{children:`pkg/ottl`}),`.`]})]}),(0,v.jsx)(d,{}),w&&(0,v.jsx)(u,{size:`xs`}),N&&!P&&!F&&(0,v.jsxs)(m,{size:`xs`,variant:`ghost`,onClick:M,children:[(0,v.jsx)(n,{size:12}),` Reset to template`]})]}),N&&P&&(0,v.jsx)(o,{borderWidth:`1px`,borderColor:`orange.300`,backgroundColor:`orange.50`,borderRadius:`md`,padding:3,children:(0,v.jsxs)(p,{alignItems:`center`,gap:3,children:[(0,v.jsx)(o,{color:`orange.600`,children:(0,v.jsx)(g,{size:18})}),(0,v.jsxs)(c,{align:`start`,gap:0,flex:1,children:[(0,v.jsx)(s,{fontSize:`sm`,fontWeight:`medium`,children:`Template available for this source type`}),(0,v.jsx)(s,{fontSize:`xs`,color:`fg.muted`,children:`Loads the canonical extraction statements maintained by LangWatch. You can customize them after loading.`})]}),(0,v.jsx)(m,{size:`sm`,colorPalette:`orange`,onClick:M,children:`Use this template`})]})}),(0,v.jsxs)(c,{align:`stretch`,gap:1,children:[P&&!N&&(0,v.jsx)(s,{fontSize:`xs`,color:`fg.muted`,fontStyle:`italic`,children:`No statements. Click “Add statement” to begin.`}),i.map((e,n)=>{let r=S[n],i=r&&!r.ok&&e.trim().length>0;return(0,v.jsxs)(o,{children:[(0,v.jsxs)(p,{alignItems:`start`,gap:2,children:[(0,v.jsx)(o,{width:`6px`,height:`6px`,borderRadius:`full`,marginTop:3,backgroundColor:e.trim()?i?`red.500`:`green.400`:`border.muted`,flexShrink:0}),(0,v.jsx)(l,{size:`sm`,rows:2,fontFamily:`mono`,fontSize:`xs`,backgroundColor:`white`,value:e,onChange:e=>k(n,e.target.value),placeholder:`set(attributes["langwatch.cost.usd"], attributes["cost_usd"]) where attributes["event.name"] == "api_request"`,borderColor:i?`red.300`:void 0}),(0,v.jsx)(h,{size:`xs`,variant:`ghost`,"aria-label":`Remove statement`,onClick:()=>A(n),children:(0,v.jsx)(t,{size:12})})]}),i&&(0,v.jsx)(s,{fontSize:`xs`,color:`red.600`,marginLeft:`14px`,marginTop:.5,children:r.message})]},n)})]}),(0,v.jsx)(p,{children:(0,v.jsxs)(m,{size:`xs`,variant:`outline`,onClick:j,children:[(0,v.jsx)(a,{size:12}),` Add statement`]})})]}):null}export{b as t};
//# sourceMappingURL=OttlEditor-DQPNrx35.js.map