/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"../../core/Error.js";import t from"../../core/Logger.js";import{isAbortError as r,throwIfNotAbortError as o}from"../../core/promiseUtils.js";import{zoomToFeature as i,zoomToClusteredFeatures as n,browseClusteredFeatures as a,removeSelectedFeature as s}from"./actions.js";const c="esri.widgets.Popup.PopupViewModel",u=()=>t.getLogger(c),l=t=>{const{event:r,view:c,viewModel:u}=t,{action:l}=r;if(!l)return Promise.reject(new e("trigger-action:missing-arguments","Event has no action"));const{disabled:d,id:g}=l;if(!g)return Promise.reject(new e("trigger-action:invalid-action","action.id is missing"));if(d)return Promise.reject(new e("trigger-action:invalid-action","Action is disabled"));if(g===i.id)return f(u).catch(o);if(g===n.id)return m(u);if(g===a.id)return u.browseClusterEnabled=!u.browseClusterEnabled,u.featureMenuOpen=u.browseClusterEnabled,Promise.resolve();if(g===s.id){u.visible=!1;const{selectedFeature:t}=u;if(!t)return Promise.reject(new e(`trigger-action:${s.id}`,"selectedFeature is required",{selectedFeature:t}));const{sourceLayer:r}=t;return r?r.remove(t):c?.graphics.remove(t),Promise.resolve()}return Promise.resolve()};function d(e){const{selectedFeature:t,location:r,view:o}=e;return o?t??r??null:null}function g(e){return!!e&&e.isAggregate&&"cluster"===e.sourceLayer?.featureReduction?.type}async function w(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 f(t,o){const{location:n,selectedFeature:a,view:s,zoomFactor:c}=t;await(o?.viewModel?.updateGeometry());const l=o?.graphic,g=l?.geometry?l:d(t);if(!s||!g){const t=new e("zoom-to:invalid-target-or-view","Cannot zoom to location without a target and view.",{target:g,view:s});throw u().error(t),t}const f=s.scale/c,m=l?.geometry??t.selectedFeature?.geometry??n,v=null!=m&&"point"===m.type&&await w(l??a,s);i.active=!0,i.disabled=!0;try{await t.zoomTo({target:{target:g,scale:v?f:void 0}})}catch(p){if(r(p))return;const t=new e("zoom-to:invalid-graphic","Could not zoom to the location of the graphic.",{graphic:l??a});u().error(t)}finally{i.active=!1,i.disabled=!1,t.zoomToLocation=null,v&&(t.location=m)}}async function m(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 u().error(t),t}if(!r||!g(r)){const t=new e("zoomToCluster:invalid-selectedFeature","Selected feature must represent an aggregate/cluster graphic.",{selectedFeature:r});throw u().error(t),t}const[i,a]=await y(o,r);n.active=!0,n.disabled=!0;const{extent:s}=await i.queryExtent(a);s&&await t.zoomTo({target:s}),n.active=!1,n.disabled=!1}async function v(e){const{view:t,selectedFeature:r}=e;if(!t||!r)return;const[o,i]=await y(t,r),{extent:n}=await o.queryExtent(i);e.selectedClusterBoundaryFeature.geometry=n,t.graphics.add(e.selectedClusterBoundaryFeature)}async function p(e){const{selectedFeature:t,view:r}=e;if(!r||!t)return;const[o,i]=await y(r,t);a.active=!0,a.disabled=!0;const{features:n}=await o.queryFeatures(i);a.active=!1,a.disabled=!1,a.value=!0;const s={features:[t].concat(n)};"feature"===e?.initialDisplayMode&&(s.featureMenuOpen=!0),e?.open(s)}async function y(e,t){const r=await e.whenLayerView(t.sourceLayer),o=r.createQuery(),i=t.getObjectId();return o.aggregateIds=null!=i?[i]:[],[r,o]}function h(e){a.value=!1;const t=e.features.filter(e=>g(e));t.length&&(e.features=t)}export{p as browseAggregateFeatures,v as displayClusterExtent,d as getSelectedTarget,g as isClusterFeature,w as isZoomScreenSize,h as removeClusteredFeaturesForBrowsing,l as triggerAction,m as zoomToClusterExtent,f as zoomToLocation};
