import{validation}from"../../Validation.js";import{isArcGisApiVersion3x}from"./ArcGisApiVersion.js";import Multipoint from"@arcgis/core/geometry/Multipoint";import Point from"@arcgis/core/geometry/Point";function computeFromExtent(e){return new Point({x:(e.xmax+e.xmin)/2,y:(e.ymax+e.ymin)/2,spatialReference:e.spatialReference})}function computeFromPolygon(e){e=isArcGisApiVersion3x?e.getCentroid():e.centroid;if(null!=e)return e}function computeFromPolyline(e){var t=e.paths;if(!(t.length<1)){let n=0,o=0,i=0,r=0;for(const s of t){let e=void 0,t=void 0;for(const f of s){var c=f[0];void 0!==e&&(a=Math.abs(c-e),n+=(c+e)*a/2,i+=a);var a,l=f[1];void 0!==t&&(a=Math.abs(l-t),o+=(l+t)*a/2,r+=a),e=c,t=l}}return new Point({x:n/i,y:o/r,spatialReference:e.spatialReference})}}function computeFromMultipoint(n){var o=n.points;if(!(o.length<1)){let e=0,t=0;for(const i of o)e+=i[0],t+=i[1];return new Point({x:e/o.length,y:t/o.length,spatialReference:n.spatialReference})}}function centroidOf(e){if(null==e)return null;switch(e.type){case"extent":return computeFromExtent(e);case"point":return e;case"polygon":return computeFromPolygon(e);case"polyline":return computeFromPolyline(e);case"multipoint":return computeFromMultipoint(e)}}class GetCentroid{execute(e,t){e=validation.array("geometry",e.geometry,validation.never);if(1<e.length){const o=new Multipoint;for(const i of e){var n=centroidOf(i);null!=n&&(o.addPoint(centroidOf(n)),o.spatialReference=i.spatialReference||o.spatialReference)}return{centroid:centroidOf(o)}}return{centroid:centroidOf(e[0])}}}GetCentroid.action="gcx:wf:arcgis::GetCentroid",GetCentroid.suite="gcx:wf:builtin";export{GetCentroid};