/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{geodesicBuffer as e,buffer as t,convexHull as r,geodesicArea as n,planarArea as o,cut as i,planarLength as s}from"../../../geometry/geometryEngine.js";import a from"../../../geometry/Polyline.js";import{load as l,project as c}from"../../../geometry/projectionUtils.js";import f from"../../../geometry/SpatialReference.js";function p(e){return"polygon"===e.type||"polyline"===e.type}function u(e){return e.hasOwnProperty("points")}function y(e){return u(e)&&2===e.points.length}class g{createBuffer(r,n,o,i){if(Array.isArray(r)){if(r.length>0){const s=f.fromJSON(r[0].spatialReference);return s.isWGS84||s.isWebMercator?e(r,n,o,i):t(r,n,o,i)}return}const s=f.fromJSON(r.spatialReference);return s.isWGS84||s.isWebMercator?e(r,n[0],o):t(r,n[0],o)}createConvexHull(e){if(Array.isArray(e)){if(1===e.length&&y(e[0])){const t=r(e[0]);if(p(t))return t}return r(e,!0).filter(p)}const t=r(e);if(p(t))return t}calculateArea(e,t){const r=new f({wkid:e.spatialReference.wkid});return r.isWGS84||r.isWebMercator?parseFloat(n(e,t).toFixed(2)):parseFloat(o(e,t).toFixed(2))}mergeAggregatedToGeometries(e){const t=[],{line:r,multipoint:n,polygon:o}=e;return r&&t.push(r),n&&t.push(n),o&&t.push(o),t}async loadProjector(){await l()}getPercentageAlong(e,t,r){const n=this._createPolyline(e.paths,r.wkid);if("point"===t.type){const e=t.x-.5,o=t.x+.5,a=[[[e,t.y-.5],[o,t.y+.5]]],l=this._createPolyline(a,t.spatialReference.wkid),f=c(l,r);if(!f)return[-1];const p=i(n,f);if(p.length>0){const e=s(n,"feet");let t;t=p[0].paths[0][0][0]===n.paths[0][0][0]?s(p[0],"feet"):p.length>1?s(p[1],"feet"):0;return[t/e]}}else{const e=c(t,r),o=i(n,e);if(o.length>0){const e=s(n,"feet");return[s(o[0],"feet")/e]}}return[-1]}async projectGeometry(e,t){return await l(),c(e,t,{zConversionDisabled:!0})}_createPolyline(e,t){return new a({hasZ:!1,hasM:!0,paths:e,spatialReference:new f({wkid:t})})}}export{g as GeometryHandler};
