var O=Object.defineProperty;var w=(t,e,n)=>e in t?O(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var c=(t,e,n)=>w(t,typeof e!="symbol"?e+"":e,n);import{Cr as L,Pt as M,Xr as P,Yr as d,ea as $,gr as U,mi as u,n as R,qr as D,ya as S}from"./cells-CJLUCcQg.js";import{g as _}from"./useEventListener-C0195JsF.js";import{n as f}from"./config-BJVx3eBW.js";import{t as k}from"./createLucideIcon-w-Qo9n0R.js";var z=k("wrench",[["path",{d:"M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.106-3.105c.32-.322.863-.22.983.218a6 6 0 0 1-8.259 7.057l-7.91 7.91a1 1 0 0 1-2.999-3l7.91-7.91a6 6 0 0 1 7.057-8.259c.438.12.54.662.219.984z",key:"1ngwbx"}]]),h,m;function F(t,e,n,a,i){var r={};return Object.keys(a).forEach(function(s){r[s]=a[s]}),r.enumerable=!!r.enumerable,r.configurable=!!r.configurable,("value"in r||r.initializer)&&(r.writable=!0),r=n.slice().reverse().reduce(function(s,o){return o(t,e,s)||s},r),i&&r.initializer!==void 0&&(r.value=r.initializer?r.initializer.call(i):void 0,r.initializer=void 0),r.initializer===void 0?(Object.defineProperty(t,e,r),null):r}function g(t){let e=t.indexOf("://");if(e!==-1)return t.slice(0,e)}var y=class{async getAttachments(t){return[]}asURI(t){return`${this.contextType}://${t}`}parseContextIds(t){let e=RegExp(`${this.mentionPrefix}([\\w-]+):\\/\\/([\\w./-]+)`,"g"),n=[...t.matchAll(e)],a=r=>r.slice(1),i=n.filter(([,r])=>r===this.contextType).map(([r])=>a(r));return[...new Set(i)]}createBasicCompletion(t,e){return{label:`${this.mentionPrefix}${t.uri.split("://")[1]}`,displayLabel:t.name,detail:(e==null?void 0:e.detail)||t.description,boost:(e==null?void 0:e.boost)||1,type:(e==null?void 0:e.type)||this.contextType,apply:`${this.mentionPrefix}${t.uri}`,section:(e==null?void 0:e.section)||this.title}}};let j=(h=$(),m=class{constructor(){c(this,"providers",new Set)}register(t){return this.providers.add(t),this}getProviders(){return this.providers}getProvider(t){return[...this.providers].find(e=>e.contextType===t)}getAllItems(){return[...this.providers].flatMap(t=>t.getItems())}parseAllContextIds(t){return[...this.providers].flatMap(e=>e.parseContextIds(t))}findProviderForUri(t){let e=g(t);if(e)return[...this.providers].find(n=>n.contextType===e&&n.getItems().some(a=>a.uri===t))}resolveItems(t){if(t.length===0)return[];let e=new f;for(let i of t){let r=g(i);r&&e.add(r,i)}let n=new Map;for(let[i,r]of e.entries()){let s=[...this.providers].filter(o=>o.contextType===i);for(let o of s){let C=new Map(o.getItems().map(l=>[l.uri,l]));for(let l of r){let p=C.get(l);p&&n.set(l,p)}}}let a=[];for(let i of t){let r=n.get(i);r&&a.push(r)}return a}getContextInfo(t){return this.resolveItems(t)}formatContextForAI(t){let e=this.resolveItems(t);return e.length===0?"":e.map(n=>{var a;return((a=this.findProviderForUri(n.uri))==null?void 0:a.formatContext(n))||""}).join(`

`)}async getAttachmentsForContext(t){let e=this.resolveItems(t);if(e.length===0)return[];let n=new f;for(let i of e){let r=this.findProviderForUri(i.uri);r&&n.add(r,i)}let a=[...n.entries()].map(async([i,r])=>{try{return await i.getAttachments(r)}catch(s){return _.error("Error getting attachments from provider",s),[]}});return(await Promise.all(a)).flat()}},F(m.prototype,"getAllItems",[h],Object.getOwnPropertyDescriptor(m.prototype,"getAllItems"),m.prototype),m);function v(t){return t.replaceAll("<","&lt;").replaceAll(">","&gt;")}function b(t){let{type:e,data:n,details:a}=t,i=`<${e}`;for(let[r,s]of Object.entries(n))if(s!==void 0){let o=v(typeof s=="object"&&s?JSON.stringify(s):String(s));i+=` ${r}="${o}"`}return i+=">",a&&(i+=v(a)),i+=`</${e}>`,i}const x={LOCAL_TABLE:7,REMOTE_TABLE:5,HIGH:4,MEDIUM:3,CELL_OUTPUT:2,LOW:2},I={ERROR:{name:"Error",rank:1},TABLE:{name:"Table",rank:2},DATA_SOURCES:{name:"Data Sources",rank:3},VARIABLE:{name:"Variable",rank:4},CELL_OUTPUT:{name:"Cell Output",rank:5},FILE:{name:"File",rank:6}};var B=S(),T="datasource",A=class extends y{constructor(e,n){super();c(this,"title","Datasource");c(this,"mentionPrefix","@");c(this,"contextType",T);this.connectionsMap=e,this.dataframes=[...n.values()].filter(a=>P(a)==="dataframe")}getItems(){return[...this.connectionsMap.values()].map(e=>{var i;let n="Database schema.",a={connection:e};return u.has(e.name)&&(a.tables=this.dataframes,n="Database schema and the dataframes that can be queried"),e.databases.length===0&&(((i=a.tables)==null?void 0:i.length)??0)===0?null:{uri:this.asURI(e.name),name:e.name,description:n,type:this.contextType,data:a}}).filter(Boolean)}formatContext(e){let n=e.data,{name:a,display_name:i,source:r,...s}=n.connection,o=s;return u.has(a)||(o={...s,engine_name:a}),b({type:this.contextType,data:{connection:o,tables:n.tables}})}formatCompletion(e){let n=e.data,a=n.connection,i=n.tables,r=a.name;return u.has(a.name)&&(r="In-Memory"),{label:`@${r}`,displayLabel:r,detail:L(a.dialect),boost:x.MEDIUM,type:this.contextType,section:I.DATA_SOURCES,info:()=>{let s=document.createElement("div");return s.classList.add("mo-cm-tooltip","docs-documentation"),(0,B.createRoot)(s).render(U(a,i)),s}}}};function E(t,e){var o;let n=(o=e.get(R(t)))==null?void 0:o.code;if(!n||n.trim()==="")return null;let[a,i,r]=M.sql.transformIn(n),s=e.get(d).connectionsMap.get(r.engine);return s?`@${T}://${s.name}`:null}function q(t,e){let n=E(t,e);if(!n)return null;let a=e.get(d).connectionsMap,i=new A(a,e.get(D)),r=n.slice(1),s=i.getItems().find(o=>o.uri===r);return s?i.formatContext(s):null}export{I as a,j as c,x as i,z as l,q as n,b as o,E as r,y as s,A as t};
