/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import e from"../../core/Error.js";import t from"../../core/Logger.js";import{isAbortError as r,throwIfNotAbortError as o}from"../../core/promiseUtils.js";import{getGraphicLayer as i}from"../../graphic/graphicOriginUtils.js";import{isGraphicsLayer as n}from"../../layers/support/layerUtils.js";import{zoomToFeature as a,zoomToClusteredFeatures as s,browseClusteredFeatures as c,removeSelectedFeature as u}from"./actions.js";const l="esri.widgets.Popup.PopupViewModel",d=()=>t.getLogger(l),g=t=>{const{event:r,view:l,viewModel:d}=t,{action:g}=r;if(!g)return Promise.reject(new e("trigger-action:missing-arguments","Event has no action"));const{disabled:w,id:f}=g;if(!f)return Promise.reject(new e("trigger-action:invalid-action","action.id is missing"));if(w)return Promise.reject(new e("trigger-action:invalid-action","Action is disabled"));if(f===a.id)return m(d).catch(o);if(f===s.id)return v(d);if(f===c.id)return d.browseClusterEnabled=!d.browseClusterEnabled,d.featureMenuOpen=d.browseClusterEnabled,Promise.resolve();if(f===u.id){d.visible=!1;const{selectedFeature:t}=d;if(!t)return Promise.reject(new e(`trigger-action:${u.id}`,"selectedFeature is required",{selectedFeature:t}));const r=i(t);return n(r)?r.remove(t):l?.graphics.remove(t),Promise.resolve()}return Promise.resolve()};function w(e){const{selectedFeature:t,location:r,view:o}=e;return o?t??r??null:null}function f(e){if(!e)return!1;const t=i(e);return e.isAggregate&&"cluster"===t?.featureReduction?.type}async function p(e,t){if("3d"!==t?.type||!e||"esri.Graphic"!==e.declaredClass)return!0;const r=t.getViewForGraphic(e);if(r&&"whenGraphicBounds"in r){let t=null;try{t=await r.whenGraphicBounds(e,{useViewElevation:!0})}catch(o){}return!t||!t.boundingBox||t.boundingBox[0]===t.boundingBox[3]&&t.boundingBox[1]===t.boundingBox[4]&&t.boundingBox[2]===t.boundingBox[5]}return!0}async function m(t,o){const{location:i,selectedFeature:n,view:s,zoomFactor:c}=t;await(o?.viewModel?.updateGeometry());const u=o?.graphic,l=u?.geometry?u:w(t);if(!s||!l){const t=new e("zoom-to:invalid-target-or-view","Cannot zoom to location without a target and view.",{target:l,view:s});throw d().error(t),t}const g=s.scale/c,f=u?.geometry??t.selectedFeature?.geometry??i,m=null!=f&&"point"===f.type&&await p(u??n,s);a.active=!0,a.disabled=!0;try{await t.zoomTo({target:{target:l,scale:m?g:void 0}})}catch(v){if(r(v))return;const t=new e("zoom-to:invalid-graphic","Could not zoom to the location of the graphic.",{graphic:u??n});d().error(t)}finally{a.active=!1,a.disabled=!1,t.zoomToLocation=null,m&&(t.location=f)}}async function v(t){const{selectedFeature:r,view:o}=t;if("2d"!==o?.type){const t=new e("zoomToCluster:invalid-view","View must be 2d MapView.",{view:o});throw d().error(t),t}if(!r||!f(r)){const t=new e("zoomToCluster:invalid-selectedFeature","Selected feature must represent an aggregate/cluster graphic.",{selectedFeature:r});throw d().error(t),t}const[i,n]=await b(o,r);s.active=!0,s.disabled=!0;const{extent:a}=await i.queryExtent(n);a&&await t.zoomTo({target:a}),s.active=!1,s.disabled=!1}async function y(e){const{view:t,selectedFeature:r}=e;if(!t||!r)return;const[o,i]=await b(t,r),{extent:n}=await o.queryExtent(i);e.selectedClusterBoundaryFeature.geometry=n,t.graphics.add(e.selectedClusterBoundaryFeature)}async function h(e){const{selectedFeature:t,view:r}=e;if(!r||!t)return;const[o,i]=await b(r,t);c.active=!0,c.disabled=!0;const{features:n}=await o.queryFeatures(i);c.active=!1,c.disabled=!1,c.value=!0;const a=[t].concat(n),s="feature"===e.initialDisplayMode,{open:u}=e;"function"!=typeof u?(e.features=a,e.featureMenuOpen=s,"boolean"==typeof u&&(e.open=!0)):u.call(e,{features:a,featureMenuOpen:s})}async function b(e,t){const r=i(t),o=await e.whenLayerView(r),n=o.createQuery(),a=t.getObjectId();return n.aggregateIds=null!=a?[a]:[],[o,n]}function F(e){c.value=!1;const t=e.features.filter(e=>f(e));t.length&&(e.features=t)}export{h as browseAggregateFeatures,y as displayClusterExtent,w as getSelectedTarget,f as isClusterFeature,p as isZoomScreenSize,F as removeClusteredFeaturesForBrowsing,g as triggerAction,v as zoomToClusterExtent,m as zoomToLocation};
