/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.25/esri/copyright.txt for details. */ import t from"../../Viewpoint.js";import e from"../../core/Collection.js";import{isSome as n,get as r,isNone as o}from"../../core/maybe.js";import{getMetersPerUnitForSR as a}from"../../core/unitUtils.js";import{t as i}from"../../chunks/common.js";import{f as c,r as s,t as u,a as f,s as l,b as m,c as y}from"../../chunks/mat2d.js";import{c as p}from"../../chunks/mat2df64.js";import{b as g,s as x,e as h,n as b,a as w,f as d,g as j,h as G,i as R,j as A,t as k,c as S}from"../../chunks/vec2.js";import{f as v,a as M}from"../../chunks/vec2f64.js";import F from"../../geometry/Extent.js";import N from"../../geometry/Geometry.js";import z from"../../geometry/Point.js";import{isLoaded as I,canProjectWithoutEngine as V,load as q,project as L}from"../../geometry/projection.js";import P from"../../geometry/SpatialReference.js";import{equals as C,isValid as E,getInfo as O}from"../../geometry/support/spatialReferenceUtils.js";const Q=96,U=39.37,B=180/Math.PI;function D(t){return t.wkid?t:t.spatialReference||P.WGS84}function T(t,e){return e.type?x(t,e.x,e.y):S(t,e)}function W(t){return a(t)}function H(t,e){const n=Math.max(1,e[0]),r=Math.max(1,e[1]);return Math.max(t.width/n,t.height/r)*ct(t.spatialReference)}async function J(t,a,i,c){let s,u;if(!t)return null;if(Array.isArray(t)&&!t.length)return null;if(e.isCollection(t)&&(t=t.toArray()),Array.isArray(t)&&t.length&&"object"==typeof t[0]){const e=t.every((t=>"attributes"in t)),r=t.some((t=>!t.geometry));let o=t;if(e&&r&&a&&a.allLayerViews){const e=new Map;for(const n of t){const t=n.layer,r=e.get(t)||[],o=n.attributes[t.objectIdField];null!=o&&r.push(o),e.set(t,r)}const r=[];e.forEach(((t,e)=>{const n=a.allLayerViews.find((t=>t.layer.id===e.id));if("queryFeatures"in n){const o=e.createQuery();o.objectIds=t,o.returnGeometry=!0,r.push(n.queryFeatures(o))}}));const i=await Promise.all(r),c=[];for(const t of i)if(t&&t.features&&t.features.length)for(const e of t.features)n(e.geometry)&&c.push(e.geometry);o=c}for(const t of o)c=await J(t,a,i,c);return c}if(Array.isArray(t)&&2===t.length&&"number"==typeof t[0]&&"number"==typeof t[1])s=new z(t);else if(t instanceof N)s=t;else if("geometry"in t)if(t.geometry)s=t.geometry;else if(t.layer){const e=t.layer,n=a.allLayerViews.find((t=>t.layer.id===e.id));if("queryFeatures"in n){const o=e.createQuery();o.objectIds=[t.attributes[e.objectIdField]],o.returnGeometry=!0;const a=await n.queryFeatures(o);s=r(a,"features",0,"geometry")}}if(o(s))return null;if(u="point"===s.type?new F({xmin:s.x,ymin:s.y,xmax:s.x,ymax:s.y,spatialReference:s.spatialReference}):s.extent,!u)return null;I()||V(u.spatialReference,i)||await q();const f=L(u,i);return f?c=c?c.union(f):f:null}function K(t){if(t&&(!Array.isArray(t)||"number"!=typeof t[0])&&("object"==typeof t||Array.isArray(t)&&"object"==typeof t[0])){if("layer"in t&&t.layer&&t.layer.minScale&&t.layer.maxScale){const e=t.layer;return{min:e.minScale,max:e.maxScale}}if(Array.isArray(t)&&t.length&&t.every((t=>"layer"in t))){let e=0,n=0;for(const r of t){const t=r.layer;t&&t.minScale&&t.maxScale&&(e=t.minScalen?t.maxScale:n)}return e&&n?{min:e,max:n}:null}}}function X(t,e){return C(D(t),e)?t:L(t,e)}async function Y(e,r){if(!e||!r)return new t({targetGeometry:new z,scale:0,rotation:0});let o=r.spatialReference;const{constraints:a,padding:i,viewpoint:c,size:s}=r,u=[i?s[0]-i.left-i.right:s[0],i?s[1]-i.top-i.bottom:s[1]];let f=null;e instanceof t?f=e:e.viewpoint?f=e.viewpoint:e.target&&"esri.Viewpoint"===e.target.declaredClass&&(f=e.target);let l=null;f&&f.targetGeometry?l=f.targetGeometry:e instanceof F?l=e:(e||e&&("center"in e||"extent"in e||"target"in e))&&(l=await J(e.center,r,o)||await J(e.extent,r,o)||await J(e.target,r,o)||await J(e,r,o)),!l&&c&&c.targetGeometry?l=c.targetGeometry:!l&&r.extent&&(l=r.extent),o||(o=D(r.spatialReference||r.extent||l)),I()||C(l.spatialReference,o)||V(l,o)||await q();const m=X(l.center?l.center:l,o);let y=null;if(f&&n(f.targetGeometry)&&"point"===f.targetGeometry.type)y=f.scale;else if("scale"in e&&e.scale)y=e.scale;else if("zoom"in e&&-1!==e.zoom&&a&&a.effectiveLODs)y=a.zoomToScale(e.zoom);else if(Array.isArray(l)||"point"===l.type||"extent"===l.type&&0===l.width&&0===l.height){const t=X(r.extent,o);y=n(t)?H(t,u):r.extent?H(r.extent,u):c.scale}else y=H(X(l.extent,o),u);const p=K(e);p&&(p.min&&p.min>y?y=p.min:p.max&&p.max