/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../Graphic.js";import o from"../../WebLinkChart.js";import n from"../../core/Collection.js";import i from"../../geometry/Extent.js";import r from"../../geometry/Point.js";import t from"../../geometry/Polyline.js";import a from"../GraphicsLayer.js";import s from"../LinkChartLayer.js";import{loadRecordsIntoLocalCache as l}from"./supportUtils.js";import m from"../support/LabelClass.js";import p from"../../renderers/visualVariables/SizeVariable.js";import y from"../../renderers/visualVariables/support/SizeStop.js";import d from"../../rest/knowledgeGraph/Entity.js";import _ from"../../rest/knowledgeGraph/Relationship.js";import c from"../../symbols/SimpleLineSymbol.js";import f from"../../symbols/SimpleMarkerSymbol.js";import w from"../../symbols/TextSymbol.js";import h from"../../views/LinkChartView.js";const b=e=>{const o=[],n={generateAllSublayers:!0,namedTypeDefinitions:new Map},i=[];return e.dataModel.entityTypes.forEach(e=>{o.push(new d({typeName:e.name,id:`ESRI__DM__ENTITY__${e.name}`,properties:e.properties.reduce((e,o)=>({...e,[o.name]:null}),{})})),n.namedTypeDefinitions.set(e.name,{useAllData:!1,members:new Map([[`ESRI__DM__ENTITY__${e.name}`,{id:`ESRI__DM__ENTITY__${e.name}`}]])})}),e.dataModel.relationshipTypes.forEach(e=>{const r=new Map;n.namedTypeDefinitions.set(e.name,{useAllData:!1,members:r}),e.endPoints.length||i.push(e.name);for(const{originEntityType:n,destinationEntityType:i}of e.endPoints)o.push(new _({typeName:e.name,id:`ESRI__DM__RELATIONSHIP__${e.name}__${n}__${i}`,originId:`ESRI__DM__ENTITY__${n}`,destinationId:`ESRI__DM__ENTITY__${i}`,properties:e.properties.reduce((e,o)=>({...e,[o.name]:null}),{})})),r.set(`ESRI__DM__RELATIONSHIP__${e.name}__${n}__${i}`,{id:`ESRI__DM__RELATIONSHIP__${e.name}__${n}__${i}`})}),{records:o,inclusionDefinition:n,unobservedRelationshipTypes:i}},u=e=>"entity"===e.graphType?[new m({labelExpression:`${e.objectType.name}`,labelPlacement:"above-center",symbol:new w({color:[255,255,255,255],font:{family:"arial-unicode-ms",size:10,weight:"bold"},haloColor:[0,0,0,255],haloSize:.7})})]:[new m({labelExpression:`${e.objectType.name}`,labelPlacement:"above-along",repeatLabel:!1,symbol:new w({color:[80,80,80],haloColor:[255,255,255],haloSize:.7,font:{size:10,weight:"normal"},yoffset:"2px"}),deconflictionStrategy:"none"})],g=e=>"entity"===e.graphType?[new p({valueExpression:"$view.scale",stops:[new y({size:30,value:0}),new y({size:6,value:1e3})]})]:[new p({valueExpression:"$view.scale",stops:[new y({size:20,value:0}),new y({size:8,value:1e3})]})],E=(o,n,s)=>{const l=5e-5,m=15e-5,p=8e-6,y=new a,d=new a({minScale:1e3,maxScale:0});let _=s.ymin;const h=s.xmin,b=h+m;for(const i of n){const n=o.layers.find(e=>e.objectType.name===i);_-=l;const a=("simple"===n?.renderer?.type?n.renderer.symbol?.clone():null)||new c({type:"simple-line",color:"gray",width:2}),s=new e({geometry:new t({paths:[[[h,_],[b,_]]]}),symbol:a});y.add(s);for(const o of[h,b]){const n=new e({geometry:new r({x:o,y:_}),symbol:new f({color:"white",outline:{color:"gray",width:1},size:6})});y.add(n)}const m=new e({geometry:new r({x:(h+b)/2,y:_+p}),symbol:new w({text:i,color:[80,80,80],haloColor:[255,255,255],haloSize:.7,font:{size:10,weight:"normal"},yoffset:"2px"})});d.add(m)}return{graphicsLayers:[y,d],graphicsExtent:new i({xmin:h,xmax:b,ymin:_,ymax:s.ymin,spatialReference:s.spatialReference})}},T=({container:e,kg:i})=>{const{records:r,inclusionDefinition:t,unobservedRelationshipTypes:a}=b(i);l(r,i.dataModel);const m=new s({title:"Data Model Visualization Layer",url:i.url,initializationInclusionModeDefinition:t,dataPreloadedInLocalCache:!0}),p=new o({layers:new n([m])}),y=new h({map:p,container:e}),d=new Promise((e,o)=>{y.when(()=>m.load().then(()=>{if(m.layers.forEach(e=>{e.labelsVisible=!0,e.visible=!0,e.labelingInfo=u(e),e.renderer&&"heatmap"!==e.renderer.type&&(e.renderer.visualVariables=g(e))}),p.diagramNodesExtent){let o=null;if(a.length>0){const{graphicsExtent:e,graphicsLayers:n}=E(m,a,p.diagramNodesExtent);p.addMany(n),o=e}const n=o?p.diagramNodesExtent.union(o):p.diagramNodesExtent;y.goTo(n.expand(1.4)).finally(()=>e())}},o),o)});return{_view:y,load:()=>d,destroy:()=>y.destroy()}};export{T as generateDataModelView,b as generateRecordsForDataModelView,u as getLabellingInfoForLayer,E as getUnobservedRelationshipGraphics,g as getVisualVariablesForLayer};
