import{C as g}from"./CreateDialogContext-Buan56-U.js";import{f as F,R as Y,j as x,aY as I}from"./vendor-ClLR6ncd.js";import{d as z}from"./Person-UulRDhfg.js";import{d as V}from"./Storage-DvymGqui.js";import{B as X}from"./Box-Co2WcNPC.js";import{a3 as b,a4 as C,a5 as Z,a8 as ee,a9 as te,w as se,aA as re,ay as O,aB as ae,aC as oe,aD as ne,H as T,i as j,b as h,u as ue,a as K,h as ie,aw as L,ar as l,q as E,d as B,ab as ce,s as ye,j as de}from"./index-OtVJl4GC.js";import"./lodash-ZyeDnt0e.js";import{o as P}from"./object_hash-DuPyIO4t.js";const v=async(e,t)=>JSON.parse(await j({url:e,...t})),le=(e,t)=>v(O(e,T(e)),t),fe=(e,t)=>v(re(e,T(e)),t),ge=(e,t)=>v(O(e,ae(e)),t),Qe=(e,t)=>v(oe(e,ne(e)),t),pe=async(e,t)=>{const{queryId:s}=e,r={...e,queryId:b(s)};switch(C(s)){case se:return le(r,t);case te:return Qe(r,t);case ee:return ge(r,t);case Z:return fe(r,t);default:throw new Error(`unsupported query type ${C(s)} from id "${s}"`)}};function me(){const{repoConnection:e,currentQuery:t}=h(),s=ue(),{getOpenedQueries:r,removeOpenedQuery:a}=K(),i=r(e);return o=>{const c=Object.values(i).filter(({id:f})=>f!==o);i[o]!==void 0&&(o===t&&s(c[c.length-1]),a(e,{id:o}))}}const{useDialogContext:M,DialogContextProvider:Ke}=g();function N(){const e=L();return t=>{l.removeQueries([...e,"query",t])}}function ve({withClosingTab:e=!0}={}){const t=N(),s=me(),r=M(),{openParameter:a,resetOpenParameter:i}=r;return ie(()=>{const o=new URL(a.uri);return j({method:"delete",url:o.href.substring(o.origin.length)})},{invalidateRepoKeysOnSettled:["queries"],onSettled:()=>{t(a.id),i()},onSuccess:()=>{e&&s(a.id)}})}const{useDialogContext:Le,DialogContextProvider:Ee}=g(),{useDialogContext:W,DialogContextProvider:Be}=g(),A=(e,t)=>{const s={...e,...t};return{queryIdToSave:s.id,newQueryId:s.newQueryId}},De=({queryIdToSave:e,newQueryId:t})=>t||e,U=(e,t)=>(e==null?void 0:e.local)||t.queryText;function xe({language:e,query:t,description:s,author:r,editors:a,title:i,queryId:o},c){return{title:i||b(o),language:e,cancelOnWarnings:!1,query:t,showLongParts:!1,type:"SPARQL",useMJQE:!1,useReasoning:!1,description:s,author:r,updateTime:new Date().valueOf(),editors:Array.isArray(a)?Array.from(new Set(a.concat(c))):[c]}}function Ce(){const e=L(),{username:t}=E(),{openParameter:s,setOpenParameter:r,resetOpenParameter:a}=W(),i=F(),o=h(),{catalogId:c,repositoryId:f,repoConnection:Q,sessionId:S,sessionPort:p}=o,{getOpenedQueries:G,removeOpenedQuery:H}=K(),m=G(Q),R=B(n=>{var w;const{queryIdToSave:u,newQueryId:y}=A(s,n),d=U(m[u],n),D=((w=m[u])==null?void 0:w.language)||n.queryLanguage;if(!d)return;const{description:J,author:_,editors:k}=Object.assign({},l.getQueryData([...e,"query",u]),n),q=De({queryIdToSave:u,newQueryId:y});return ye({username:t,catalogId:c,repositoryId:f,sessionId:S,sessionPort:p,queryId:q},xe({language:D,query:d,description:J,author:_,editors:k,queryId:q},t))},{onError:n=>{console.error(n)},onSettled:()=>{a()},onSuccess:(n,u)=>{const{queryIdToSave:y,newQueryId:d}=A(s,u);if(l.invalidateQueries([...e,"queries"]),l.invalidateQueries([...e,"query",y]),l.invalidateQueries([...e,"query",d]),d){const D=U(m[y],u);l.setQueryData([...e,d,P(D),"exec"],l.getQueryData([...e,y,P(""),"exec"])),m[y]&&(H(Q,{id:y}),i(ce(o)({id:d})))}}});return{saveQuery:n=>{r(u=>({...u,...n})),R.mutate(n)},saveQueryAsync:async n=>{r(u=>({...u,...n})),await R.mutateAsync(n)}}}const he=z,Se=V,Me=Y.forwardRef(function(t,s){return x.jsxs(X,{component:"span",ref:s,style:{...t.style,position:"relative",height:"1.5rem",width:"1.5rem"},...t,children:[x.jsx(Se,{sx:{color:"grey.500"}}),x.jsx(he,{sx:{position:"absolute",left:0,top:0}})]})});function Re(e){const{username:t}=E(),s=h();return de(["query",e],async()=>{const r=await pe({username:t,...s,queryId:e});return{queryId:e,...r}},{enabled:!!C(e)})}function $(e,t=!1){const{openParameter:s,resetOpenParameter:r}=e(),a=Re(s==null?void 0:s.id),{saveQueryAsync:i}=Ce(),o=ve({withClosingTab:!1}),c=N(),{setOpenParameter:f}=W(),{setOpenParameter:Q}=M();return{mutate:B(async p=>{I.flushSync(()=>{f(s)}),await i({...p,queryText:a.data.query,queryLanguage:a.data.language}),t&&s.id!==p.newQueryId?(I.flushSync(()=>{Q(s)}),await o.mutateAsync(s)):c(s.id)},{onSettled:()=>{r()}}).mutate,disabled:a.isLoading}}const{useDialogContext:qe,DialogContextProvider:Ne}=g();function We(){return $(qe,!0)}const{useDialogContext:we,DialogContextProvider:$e}=g();function Ge(){return $(we)}export{Ke as D,Se as R,$e as S,Me as U,ve as a,Le as b,me as c,Ce as d,W as e,Re as f,he as g,We as h,qe as i,Ge as j,we as k,Ne as l,Be as m,Ee as n,pe as o,xe as q,M as u};
