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