/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../config.js";import r from"../ArcadePortal.js";import t from"../Dictionary.js";import{ArcadeExecutionError as n}from"../executionError.js";import{B as o,s as a,o as i,O as s,P as p,e as c,f as l,g as f,n as u,h as m}from"../../chunks/languageUtils.js";import{getPortal as d}from"../portalUtils.js";import h from"../../geometry/Geometry.js";import{load as g,project as w,isLoaded as y}from"../../geometry/projectionUtils.js";import j from"../../geometry/SpatialReference.js";import{webMercatorToGeographic as S,geographicToWebMercator as R}from"../../geometry/support/webMercatorUtils.js";import G from"../../portal/PortalItem.js";import{project as v}from"../../rest/geometryService/project.js";import x from"../../rest/knowledgeGraph/Entity.js";import k from"../../rest/knowledgeGraph/GraphQueryStreaming.js";import P from"../../rest/knowledgeGraph/ObjectValue.js";import b from"../../rest/knowledgeGraph/Path.js";import I from"../../rest/knowledgeGraph/Relationship.js";import W from"../../rest/support/ProjectParameters.js";import{isString as T,isArray as D,isNumber as U,isDate as q}from"../../support/guards.js";let A=null;async function N(r){const t=e.geometryServiceUrl??"";if(!t){y()||await g();for(const e of r)e.container[e.indexer]=w(e.container[e.indexer],j.WGS84);return}const n=r.map(e=>e.container[e.indexer]),o=new W({geometries:n,outSpatialReference:j.WGS84}),a=await v(t,o);for(let e=0;e<a.length;e++){const t=r[e];t.container[t.indexer]=a[e]}}async function F(e,r){const t=new G({portal:e,id:r});return await t.load(),null===A&&(A=await import("../../rest/knowledgeGraphService.js")),await A.fetchKnowledgeGraph(t.url)}function J(e,r,t,n,o){if(null===e)return null;if(T(e)||U(e))return e;if(c(e))return e.toJSDate();if(c(e))return e.toJSDate();if(l(e))return e.toStorageFormat();if(f(e))return e.toStorageString();if(u(e)){const a={};for(const i of e.keys())a[i]=J(e.field(i),r,t,n,o),a[i]instanceof h&&o.push({container:a,indexer:i});return a}if(D(e)){const a=e.map(e=>J(e,r,t,n,o));for(let e=0;e<a.length;e++)a[e]instanceof h&&o.push({container:a,indexer:e});return a}return m(e)?e.spatialReference.isWGS84?e:e.spatialReference.isWebMercator&&r?S(e):e:void 0}function M(e,r){if(!e)return e;if(e.spatialReference.isWGS84&&r.spatialReference.isWebMercator)return R(e);if(e.spatialReference.equals(r.spatialReference))return e;throw new n("WrongSpatialReference",null)}function Q(e,r){if(!e)return null;const t={};for(const n in e)t[n]=B(e[n],r);return t}function B(e,r){return null===e?null:D(e)?e.map(e=>B(e,r)):e instanceof x?{graphTypeName:e.typeName,id:e.id,graphType:"entity",properties:Q(e.properties,r)}:e instanceof P?{graphType:"object",properties:Q(e.properties,r)}:e instanceof I?{graphTypeName:e.typeName,id:e.id,graphType:"relationship",originId:e.originId??null,destinationId:e.destinationId??null,properties:Q(e.properties,r)}:e instanceof b?{graphType:"path",path:e.path?e.path.map(e=>B(e,r)):null}:m(e)?M(e,r):T(e)||U(e)||q(e)?e:null}function E(e){"async"===e.mode&&(e.functions.knowledgegraphbyportalitem=function(t,i){return e.standardFunctionAsync(t,i,(e,s,p)=>{if(o(p,2,2,i),!(p[0]instanceof r))throw new n("PortalRequired",i);const c=a(p[1]);return F(d(t,p[0]),c)})},e.signatures.push({name:"knowledgegraphbyportalitem",min:2,max:2}),e.functions.querygraph=function(r,a){return e.standardFunctionAsync(r,a,async(e,c,l)=>{o(l,2,4,a);const f=l[0];if(!i(f))throw new n("InvalidParameter",a);const u=l[1];if(!T(u))throw new n("InvalidParameter",a);null===A&&(A=await import("../../rest/knowledgeGraphService.js"));let m=null;const d=s(l[2],null);if(!(d instanceof t||null===d))throw new n("InvalidParameter",a);if(d){let e=[];m=J(d,!0,!1,r,e),e=e.filter(e=>!e.container[e.indexer].spatialReference.isWGS84),e.length>0&&await N(e)}const h=s(l[3],!1),g=new k({openCypherQuery:u,bindParameters:m,provenanceBehavior:h?"include":"exclude"});(f?.serviceDefinition?.currentVersion??11.3)>11.2&&(g.outputSpatialReference=r.spatialReference);const w=(await A.executeQueryStreaming(f,g)).resultRowsStream.getReader(),y=[];try{for(;;){const{done:e,value:t}=await w.read();if(e)break;if(D(t))for(const n of t)y.push(B(n,r));else{const e=[];for(const n of t)e.push(B(t[n],r));y.push(e)}}}catch(j){throw j}return t.convertJsonToArcade(y,p(r),!1,!0)})},e.signatures.push({name:"querygraph",min:2,max:4}))}export{E as registerFunctions};
