/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{estimateAttributesMemory as t}from"../../../../core/memoryEstimations.js";import{empty as e,width as i,height as s}from"../../../../geometry/support/aaBoundingRect.js";import{getBoundsXY as r}from"../../../../geometry/support/boundsUtils.js";import{closeRingsAndFixWinding as o}from"../../../../geometry/support/coordsUtils.js";import{isExtent as n}from"../../../../geometry/support/jsonTypeUtils.js";import{normalizeCentralMeridianForDisplay as m}from"../../../../geometry/support/normalizeUtilsSync.js";import{isCurvedGeometry as a}from"../../../../geometry/support/curves/curveUtils.js";import{densifyCurvedGeometry as h}from"../../../../geometry/support/curves/densifyCurvedGeometry.js";import{estimateGeometryMemory as u}from"../../../../layers/graphics/dehydratedFeatures.js";import{checkProjectionSupport as y,project as c}from"../../../../layers/graphics/data/projectionSupport.js";import{getCoarseSegmentsPerCurve as p,getMinSegmentsPerCurve as l,getMaxDeviationInPixels as d,getApproximateMaxDensificationSegments as f}from"./densificationConstants.js";import{polygonFromExtent as g}from"./graphicsUtils.js";class v{static fromGraphic(t,e,i,s){return new v(t.geometry,e,t.attributes,t.visible,t.uid,t.version,i,s)}constructor(t,i,s,r,o,n,m,a){this.geometry=t,this.symbol=i,this.attributes=s,this.visible=r,this.objectId=o,this._version=n,this.zOrder=m,this.displayId=a,this.symbolBounds=e(),this.prevSymbolBounds=e(),this.size=[0,0,0,0],this.geometryBounds=e(),this._isDensificationDirty=!1,this._densificationDeviation=1/0}get projectedGeometry(){return this._projectedGeometry}get linearCIM(){return this.symbolResource?.symbolInfo.linearCIM}get usedMemory(){return 128+t(this.attributes)+u(this.geometry)}get hasAnimations(){const{linearCIM:t}=this;return!!t&&t.some(t=>"animationParams"in t&&!!t.animationParams)}get hasCurvedGeoemtry(){return null!=this.geometry&&"mesh"!==this.geometry.type&&a(this.geometry)}update(t,i,s){return(this._version!==t.version||this.zOrder!==s||this.symbol!==i)&&(this.prevSymbolBounds=this.symbolBounds,this.symbolBounds=e(),this.zOrder=s,this.geometry=t.geometry,this.attributes=t.attributes,this.symbol=i,this.visible=t.visible,this._version=t.version,this.symbolResource=null,this._projectedGeometry=null,e(this.geometryBounds),this._minDensificationDeviation=null,this._isDensificationDirty=!0,!0)}updateDensificationResolution(t){if(!this.hasCurvedGeoemtry)return!1;const e=Math.max(D(t),this._minDensificationDeviation??0);return e!==this._densificationDeviation&&(this._densificationDeviation=e,this._isDensificationDirty=!0,!0)}async projectAndNormalize(t){let e,o=this.geometry;if(o&&o.spatialReference&&"mesh"!==o.type&&("extent"===o.type&&(o=g(o)),e=a(o)?h(o,{maxSegmentLength:1/0,minSegmentsPerCurve:p()}):o.toJSON(),this._projectedGeometry=await j(e,o.spatialReference,t),r(this.geometryBounds,this._projectedGeometry),this.hasCurvedGeoemtry&&null!=this._projectedGeometry)){const t=i(this.geometryBounds),e=s(this.geometryBounds);this._minDensificationDeviation=b(t,e)}}async densifyCurvedGeometryForDisplay(t){if(!this.hasCurvedGeoemtry||!this._isDensificationDirty)return;this._isDensificationDirty=!1;const e=this.geometry;if(!e||!e.spatialReference||"mesh"===e.type)return;const i=t.metersPerUnit/e.spatialReference.metersPerUnit,s=this._densificationDeviation*i,r=h(e,{maxDeviation:s,minSegmentsPerCurve:l()});this._projectedGeometry=await j(r,e.spatialReference,t)}}async function j(t,e,i){await y(t.spatialReference,i);const s=m(t);if(!s)return;const r=c(s,e,i);return r&&o(r),n(r)?g(r):r}function D(t){return 2**Math.round(Math.log2(t))*d()}function b(t,e){if(t>0&&e>0){const i=Math.min(t,e)/2,s=2*Math.PI/f();return i*(1-Math.cos(s/2))}return null}export{v as GraphicStoreItem};
