/* COPYRIGHT Esri - https://js.arcgis.com/5.1/LICENSE.txt */
import b from"./EEUNU5QU.js";import a from"./ZHZFKZA7.js";import{h as P}from"./UE4FNMRR.js";import{K as I}from"./4BNI5KJQ.js";export default $arcgis.t(([N,{B:S},{a:q,d:F,e:T,f:g,g:h,h:C,i:$,j:x,n:L,o:_,p:D}])=>{function b(t){for(let e of t){if(e instanceof L)return e;if(e.isCollection&&"variables"in e){let n=b(e.variables.filter(a=>a.type!=="group"));if(n)return n}}}function A(t,e){return`${t}:${e}`}function V(t){if(t){for(let e of t)if(e.type==="dataCatalogDatastore")return e.definition}}async function B(t){let e=new N({url:t.portal.url});return await e.load(),new q({itemId:t.id,serviceUrl:t.serviceUrl,portal:e})}async function E(t){return S(t)?await B(t):t}function K(t,e,n){let a=`**${t}**`;return e.type&&(a+=`  
${e.type}`),n&&(a+=`  
Datastore: ${n}`),{value:a}}function z(t,e){let n=`**${t.name}**`;return t.serverType&&(n+=`  
${t.serverType}`),n+=`  
Nullable: ${t.nullable?"yes":"no"}`,n+=`  
Table: ${e}`,{value:n}}function U(t,e,n){return t.map(a=>{let o=C(a.name,n);return{name:a.name,normalizedName:h(a.name),quotedName:o===a.name?void 0:o,dataType:a.serverType,nullable:a.nullable,completionLabel:a.name,insertText:o,detail:[a.serverType,a.nullable?"nullable":"not null"].filter(Boolean).join(" | "),documentation:z(a,e)}})}function k(t,e,n,a){let o=$(t.name,a),r=t.name.split("."),s=x(t.name);return{id:A(e,t.name),name:t.name,normalizedName:h(t.name),quotedName:o===t.name?void 0:o,description:t.type,schema:r.length>=2?r.at(-2):void 0,catalog:r.length>=3?r.at(-3):void 0,completionLabel:t.name,insertText:o,detail:[t.type,n].filter(Boolean).join(" | "),documentation:K(s,t,n),columns:[]}}async function W(t,e){let n=b(t.dictionaryVariables);if(!n)return{tables:[]};await n.loadSource();let a=n.datastoreInfo,o=a?.datastoreId;if(!o)return{tables:[]};let r=n.tableVariables.map(s=>k(s.datasetInfo,o,a?.name,e));return{datastore:{id:o,displayName:a?.name,databasePlatform:a?.databasePlatform},tables:r}}async function Z(t){let e=V(t?.variables);if(!e)return g(void 0);let n=await(await E(e)).fetchInfo();return g(n.databasePlatform)}var y=class extends D{#e=new Map;#t=new Map;#a;constructor(e){super(e,{apiPath:"./assets/sql-layer/api",apiPrefix:"sql-layer-api.t9n."});let n=F(e);this.dialectId=n}getWorkerHostExtension(){return{getSqlLayerDialectSpec:async()=>await this.getDialectSpec(),getSqlLayerColumnsForModel:async(e,n)=>await this._loadSqlLayerColumnsForModel(e,n)}}async getDialectSpec(){return this.#a??=T(this.languageId),await this.#a}disposeForModel(e){this.#t.delete(this._getApiKey(e)),super.disposeForModel(e)}async setProfileForModel(e,n,a){let o=this._getApiKey(e),r=(this.#e.get(o)||Promise.resolve()).then(async()=>{let i=await P(a.locale,I("./assets/editor-profile/t9n"),"messages.");if(!i)throw new Error(`Failed to load the language bundle for ${a.locale}`);this.disposeForModel(e);let u=new _(n,i,a.locale);this._modelToProfileMap.set(o,u);let f=await W(u,await this.getDialectSpec());this.updateApiContextForModel(e,{locale:a.locale,snippets:a.snippets,sqlProfile:f})}),s=r.finally(()=>{this.#e.get(o)===s&&this.#e.delete(o)});return this.#e.set(o,s),await r}async _loadSqlLayerColumnsForModel(e,n){if(!n.length)return[];let a=this._getApiKey(e),o=this.getApiContextForModel(e),r=o.sqlProfile,s=this._modelToProfileMap.get(a),i=s?b(s.dictionaryVariables):void 0;if(!r||!i)return[];await i.loadSource();let u=i.datastoreInfo?.datastoreId;if(!u)return[];let f=new Map(i.tableVariables.map(l=>[A(u,l.datasetInfo.name),l])),d=this.#t.get(a);d||(d=new Map,this.#t.set(a,d));let w=await Promise.all(n.map(async l=>{let M=r.tables.find(c=>c.id===l);if(M?.columns.length)return{tableId:l,columns:M.columns};let m=d.get(l);return m||(m=(async()=>{let c=f.get(l);return c?(await c.loadSource(),U(c.fieldInfos,c.datasetInfo.name,await this.getDialectSpec())):[]})(),d.set(l,m)),{tableId:l,columns:await m}})),v=this._mergeLoadedColumns(r,w);return v!==r&&this.updateApiContextForModel(e,{...o,sqlProfile:v}),w}_mergeLoadedColumns(e,n){let a=new Map(n.map(s=>[s.tableId,s.columns])),o=!1,r=e.tables.map(s=>s.columns.length||!a.has(s.id)?s:(o=!0,{...s,columns:a.get(s.id)??s.columns}));return o?{...e,tables:r}:e}},p=new Map;function G(t){let e=p.get(t);return e||(e=new y(t),p.set(t,e)),e}function J(t){return p.get(t)}function O(){return[...p.values()]}return{a:Z,b:G,c:J,d:O}},"portal/Portal",a,b)