/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../core/Error.js";import i from"../../renderers/SimpleRenderer.js";import a from"../heuristics/outline.js";import r from"../heuristics/sizeRange.js";import{spliceVisualVariables as n,processRegenerateParams as o,getRendererToUpdate as t,getStyleType as l,hasOutlineVV as s,hasScaleDependentSizeVV as m,findOutlineVVIndex as p,findScaleDependentSizeVVIndex as c}from"./support/regenerateUtils.js";import{errorCallback as u,createSymbol as d,getSymbolOutlineFromScheme as y,getSymbolSizeFromScheme as b,getBasemapInfo as f}from"./support/utils.js";import{verifyBinningParams as w}from"../support/binningUtils.js";import{binningCapableLayerTypes as h,featureCapableLayerTypes as g,createLayerAdapter as v,getLayerTypeLabels as T}from"../support/adapters/support/layerUtils.js";import{cloneScheme as z,getSchemes as E}from"../symbology/location.js";async function S(i){if(!i?.layer)throw new e("location-renderer:missing-parameters","'layer' parameter is required");i.forBinning&&w(i,"location-renderer");const a={...i,layer:i.layer};a.symbolType=a.symbolType||"2d";const r=i.forBinning?h:g,n=v(a.layer,r,i.forBinning);if(!n)throw new e("location-renderer:invalid-parameters","'layer' must be one of these types: "+T(r).join(", "));a.layer=n;const o=null!=a.signal?{signal:a.signal}:null;await n.load(o);const t=n.geometryType;a.outlineOptimizationEnabled="polygon"===t&&a.outlineOptimizationEnabled;const l="featureReduction"in i.layer&&"cluster"===i.layer.featureReduction?.type;if(a.sizeOptimizationEnabled=!("point"!==t&&"multipoint"!==t&&"polyline"!==t||l)&&a.sizeOptimizationEnabled,"mesh"===t)a.symbolType="3d-volumetric",a.colorMixMode=a.colorMixMode||"replace",a.edgesType=a.edgesType||"none";else{if("3d-volumetric-uniform"===a.symbolType&&"point"!==t)throw new e("location-renderer:not-supported","3d-volumetric-uniform symbols are supported for point layers only");if(a.symbolType.includes("3d-volumetric")&&"3d"!==a.view?.type)throw new e("location-renderer:invalid-parameters","'view' parameter should be an instance of SceneView when 'symbolType' parameter is '3d-volumetric' or '3d-volumetric-uniform'")}return a}async function j(i){const a="regenerate-location-renderer";await o(i,a);const r=await t(i);if("location"!==l(r))throw new e(`${a}:invalid-parameters`,"Renderer is invalid");const{layer:n,forBinning:p,filter:c,view:u,signal:d}=i,y=s(r),b=m(r),f=await S({layer:n,forBinning:p,filter:c,view:u,signal:d,outlineOptimizationEnabled:y,sizeOptimizationEnabled:b});return{...i,creatorParameters:f,renderer:r}}async function I(e,i){let a=e.locationScheme,r=null,n=null;const o=await f(e.basemap,e.view);if(r=null!=o.basemapId?o.basemapId:null,n=null!=o.basemapTheme?o.basemapTheme:null,a)return{scheme:z(a),basemapId:r,basemapTheme:n};const t=E({basemapTheme:n,geometryType:i,worldScale:!!e.symbolType?.includes("3d-volumetric"),view:e.view});return t&&(a=t.primaryScheme,r=t.basemapId,n=t.basemapTheme),{scheme:a,basemapId:r,basemapTheme:n}}async function O(n){const o=await S(n),t=o.layer.geometryType,l=await I(o,t),s=l.scheme;if(!s)throw new e("location-renderer:insufficient-info","Unable to find location scheme");const{view:m,layer:p,signal:c,filter:f}=o,[w,h]=await Promise.all([o.outlineOptimizationEnabled?a({view:m,layer:p,signal:c,filter:f}).catch(u):null,o.sizeOptimizationEnabled?r({view:m,layer:p,signal:c,filter:f}).catch(u):null]),g=w?.opacity,v=new i({symbol:d(t,{type:o.symbolType,color:s.color,size:b(s,t),outline:y(s,t,g),meshInfo:{colorMixMode:o.colorMixMode,edgesType:o.edgesType}})});return w?.visualVariables.length&&(v.visualVariables=w.visualVariables.map(e=>e.clone())),h?.minSize&&(v.visualVariables?v.visualVariables.push(h.minSize):v.visualVariables=[h.minSize]),{renderer:v,locationScheme:z(s),basemapId:l.basemapId,basemapTheme:l.basemapTheme}}async function M(i){const{creatorParameters:o,view:t,signal:l,filter:s,renderer:m}=await j(i),{layer:d,outlineOptimizationEnabled:y,sizeOptimizationEnabled:b}=o,f=o.layer.geometryType,w=await I(o,f),h=w.scheme;if(!h)throw new e("location-renderer:insufficient-info","Unable to find location scheme");const[g,v]=await Promise.all([y?a({view:t,layer:d,signal:l,filter:s}).catch(u):null,b?r({view:t,layer:d,signal:l,filter:s}).catch(u):null]);return n(m,g?.visualVariables,p),n(m,v?.minSize,c),{renderer:m,locationScheme:z(h),basemapId:w.basemapId,basemapTheme:w.basemapTheme}}export{O as createRenderer,M as regenerateRenderer};
