/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../../core/Logger.js";import{subtract as t,add as r}from"../../../core/libs/gl-matrix-2/math/vec3.js";import{create as i,fromValues as o}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{projectPointToVector as n}from"../../projection/projectPointToVector.js";import s from"../MeshGeoreferencedVertexSpace.js";import c from"../MeshLocalVertexSpace.js";import{isRelativeVertexSpace as a}from"../meshVertexSpaceUtils.js";import{performGlobalOperation as f}from"./geographicUtils.js";import{logProjectionError as p,loadProjectErrorMessage as l}from"./projection.js";import{convertVertexSpace as m}from"./vertexSpaceConversion.js";const g=()=>e.getLogger("esri.geometry.support.meshUtils.centerAt");function u(e,t,r){if(!e.vertexAttributes?.position)return;const{vertexSpace:i}=e,o=r?.origin??e.origin;if(a(i))x(e,t,o);else{f(i,o.spatialReference)?v(e,t,o):R(e,t,o)}}function x(e,o,s){const{vertexSpace:c}=e;if(!a(c))return;const f=h,m=b;if(!n(o,m,e.spatialReference))return void p(g(),o.spatialReference,e.spatialReference,l);if(!n(s,f,e.spatialReference))return void p(g(),s.spatialReference,e.spatialReference,l);const u=t(A,m,f);c.origin=r(i(),c.origin,u)}function v(e,t,r){const i=o(r.x,r.y,r.z??0),n=m(e,new c({origin:i}));if(!n)return;const a=o(t.x,t.y,t.z??0),f=m({vertexAttributes:n,spatialReference:e.spatialReference,vertexSpace:new c({origin:a})},s.absolute);if(!f)return;const{position:p,normal:l,tangent:g}=f;e.vertexAttributes.position=p,e.vertexAttributes.normal=l,e.vertexAttributes.tangent=g,e.vertexAttributesChanged()}function R(e,t,r){const i=h,o=b;if(n(t,o,e.spatialReference)){if(!n(r,i,e.spatialReference)){const t=e.origin;return i[0]=t.x,i[1]=t.y,i[2]=t.z,void p(g(),r.spatialReference,e.spatialReference,l)}j(e.vertexAttributes.position,o,i),e.vertexAttributesChanged()}else p(g(),t.spatialReference,e.spatialReference,l)}function j(e,t,r){if(e)for(let i=0;i<e.length;i+=3)for(let o=0;o<3;o++)e[i+o]+=t[o]-r[o]}const b=i(),h=i(),A=i();export{u as centerAt};
