/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{rad2deg as e}from"../../../../../core/mathUtils.js";import t from"../../../../../geometry/Point.js";import{normalizeTransform as r,quantizeY as i,quantizeX as n}from"../../../../../geometry/support/quantizationUtils.js";import{convertFromFeatures as s,convertFromFeatureSet as o}from"../../../../../layers/graphics/featureConversionUtils.js";import a from"../../../../../layers/graphics/OptimizedGeometry.js";import{project as u}from"../../../../../layers/graphics/data/projectionSupport.js";import m from"../../../../../layers/support/FieldsIndex.js";import{FeatureSetReaderJSON as f}from"./FeatureSetReaderJSON.js";import{DimensionRealFieldName as d}from"../../support/annodime/dimensionUtils.js";import{getAttributeValue as p}from"../../support/annodime/utils.js";class c extends f{static fromFeatures(e,t){const{geometryType:r}=t,i=s([],e,r,!1,!1,t.featureIdInfo);for(let n=0;n<i.length;n++)i[n].displayId=e[n].displayId;return c.fromOptimizedFeatures(i,t,null)}static fromFeatureSet(e,t){const r=o(e,t.featureIdInfo);return c.fromOptimizedFeatureSet(r,t)}static fromOptimizedFeatureSet(e,t){const r=c.fromOptimizedFeatures(e.features,t,e.transform);return r._exceededTransferLimit=e.exceededTransferLimit,r._fieldsIndex=new m(e.fields),r}static fromOptimizedFeatures(e,t,r){const i=new c(e,t);i._fieldsIndex=t.fieldsIndex;const{layerType:n}=t;if(!n)throw new Error("Layer type is required in metadata to read dimension features.");if(!r)throw new Error("Quantization transform is required to read dimension features.");return l(i._features,r,t),i}}function l(s,o,m){const f=m.spatialReference,c=m.outSpatialReference,l=r(o),y={x:0,y:0,spatialReference:f},x={x:0,y:0,spatialReference:f},h={x:0,y:0,spatialReference:f},N={x:0,y:0,spatialReference:f};let I=0;for(const r of s){const{attributes:s}=r;if(y.x=Number(p(d.beginX,s)),y.y=Number(p(d.beginY,s)),x.x=Number(p(d.endX,s)),x.y=Number(p(d.endY,s)),h.x=Number(p(d.dimX,s)),h.y=Number(p(d.dimY,s)),isNaN(y.x)||isNaN(y.y)||isNaN(x.x)||isNaN(x.y)){console.error(`Feature with ${d.objectId}=${p(d.objectId,s)} is missing valid begin, end, or dimension points and will be skipped.`);continue}(isNaN(h.x)||isNaN(h.y))&&(h.x=y.x,h.y=y.y);const o=u(new t(y),f,c),m=u(new t(x),f,c),M=[o,m,u(new t(h),f,c)];I=Math.sqrt((m.x-o.x)*(m.x-o.x)+(m.y-o.y)*(m.y-o.y));const b=m.x-o.x,_=m.y-o.y;let g=Math.PI/2;if(0!==b?g=Math.atan2(_,b):_<0&&(g=-Math.PI/2),null!=s[d.textX]&&null!=s[d.textY]){N.x=Number(p(d.textX,s)),N.y=Number(p(d.textY,s));const e=u(new t(N),f,c);M.push(e)}const j=M.map(e=>({x:n(l,e.x),y:i(l,e.y)})).flatMap(e=>[e.x,e.y]);s.__SYSTEM_LENGTH__=I,s.__DIMENSION_ANGLE__=360-e(g),r.geometry=new a([j.length],j)}}function y(e,t,r){const i=t[0]-e[0],n=t[1]-e[1],s=Math.sqrt(i*i+n*n);if(0===s)return[e[0],e[1]];return[e[0]+i/s*r,e[1]+n/s*r]}const x=1e-9;function h(e,t){return e[0]*t[1]-e[1]*t[0]}function N(e,t,r,i){const n=[Math.cos(t),Math.sin(t)],s=[Math.cos(i),Math.sin(i)],o=[r[0]-e[0],r[1]-e[1]],a=h(n,s);if(Math.abs(a)<=x)return null;const u=h(o,s)/a,m=h(o,n)/a;if(u<0||m<0)return null;return[e[0]+n[0]*u,e[1]+n[1]*u]}function I(e){const t=e[0],r=e[1];if(0===t&&0===r)return 0;if(0===t&&r>0)return Math.PI/2;if(0===t&&r<0)return 3*Math.PI/2;if(t<0&&0===r)return Math.PI;if(t>0&&0===r)return 0;let i=Math.atan2(r,t);return i<0&&(i+=2*Math.PI),i}export{c as FeatureSetReaderDimensionJSON,I as angleFromXAxis,N as intersectRaysByPointsAndAngles,l as loadDimensionFeatures,y as queryPointOnLine};
