/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../chunks/_rollupPluginBabelHelpers","../../core/Error","../../core/Logger","../../core/promiseUtils","./actions"],(function(e,t,r,o,i,n){"use strict";const s="esri.widgets.Popup.PopupViewModel",a=o.getLogger(s),u=e=>{const{event:t,view:o}=e,{action:s}=t,a=o&&o.popup;if(!s)return Promise.reject(new r("trigger-action:missing-arguments","Event has no action"));if(!a)return Promise.reject(new r("trigger-action:missing-arguments","view.popup is missing"));const{disabled:u,id:c}=s;if(!c)return Promise.reject(new r("trigger-action:invalid-action","action.id is missing"));if(u)return Promise.reject(new r("trigger-action:invalid-action","Action is disabled"));if(c===n.zoomToFeature.id)return g(a.viewModel).catch(i.throwIfNotAbortError);if(c===n.zoomToClusteredFeatures.id)return w(a.viewModel);if(c===n.browseClusteredFeatures.id)return a.featureMenuOpen=!a.featureMenuOpen,a.viewModel.browseClusterEnabled=!a.viewModel.browseClusterEnabled,Promise.resolve();if(a.viewModel.browseClusterEnabled=!1,c===n.removeSelectedFeature.id){a.close();const{selectedFeature:e}=a;if(!e)return Promise.reject(new r(`trigger-action:${n.removeSelectedFeature.id}`,"selectedFeature is required",{selectedFeature:e}));const{sourceLayer:t}=e;return t?t.remove(e):o.graphics.remove(e),Promise.resolve()}return Promise.resolve()};function c(e){const{selectedFeature:t,location:r,view:o}=e;if(!o)return null;if("3d"===o.type)return t||r;return e.get("selectedFeature.geometry")||r}function l(e){var t,r;return(null==e?void 0:e.isAggregate)&&"cluster"===(null==e||null==(t=e.sourceLayer)||null==(r=t.featureReduction)?void 0:r.type)}function d(e,t){if("3d"!==(null==t?void 0:t.type)||!e||"esri.Graphic"!==e.declaredClass)return!0;const o=t.getViewForGraphic(e);if(o&&"whenGraphicBounds"in o){let t=!1;return o.whenGraphicBounds(e,{useViewElevation:!0}).then((e=>{t=!e||!e.boundingBox||e.boundingBox[0]===e.boundingBox[3]&&e.boundingBox[1]===e.boundingBox[4]&&e.boundingBox[2]===e.boundingBox[5]})).catch((()=>{const t=new r("zoom-to:invalid-graphic","Could not zoom to the location of the graphic.",{graphic:e});a.error(t)})),t}return!0}function g(e){return p.apply(this,arguments)}function p(){return(p=t._asyncToGenerator((function*(e){const{location:t,selectedFeature:o,view:i,zoomFactor:s}=e,u=c(e);if(!u){const e=new r("zoom-to:invalid-target-or-view","Cannot zoom to location without a target and view.",{target:u,view:i});throw a.error(e),e}const l=i.scale/s,g=e.get("selectedFeature.geometry")||t,p=g&&"point"===g.type&&d(o,i);n.zoomToFeature.active=!0,n.zoomToFeature.disabled=!0;try{yield e.view.goTo({target:u,scale:p?l:void 0})}finally{n.zoomToFeature.active=!1,n.zoomToFeature.disabled=!1,e.zoomToLocation=null,p&&(e.location=g)}}))).apply(this,arguments)}function w(e){return v.apply(this,arguments)}function v(){return(v=t._asyncToGenerator((function*(e){const{selectedFeature:t,view:o}=e;if("2d"!==(null==o?void 0:o.type)){const e=new r("zoomToCluster:invalid-view","View must be 2d MapView.",{view:o});throw a.error(e),e}if(!l(t)){const e=new r("zoomToCluster:invalid-selectedFeature","Selected feature must represent an aggregate/cluster graphic.",{selectedFeature:t});throw a.error(e),e}const i=t.sourceLayer,s=yield o.whenLayerView(i),u=s.createQuery();u.aggregateIds=[t.getObjectId()],n.zoomToClusteredFeatures.active=!0,n.zoomToClusteredFeatures.disabled=!0;const{extent:c}=yield s.queryExtent(u);yield o.goTo({target:c}),n.zoomToClusteredFeatures.active=!1,n.zoomToClusteredFeatures.disabled=!1}))).apply(this,arguments)}function f(e){return y.apply(this,arguments)}function y(){return(y=t._asyncToGenerator((function*(e){const{selectedFeature:t,view:r}=e,o=t.sourceLayer,i=yield r.whenLayerView(o),n=i.createQuery();n.aggregateIds=[t.getObjectId()];const{extent:s}=yield i.queryExtent(n);e.selectedClusterBoundaryFeature.geometry=s,r.graphics.add(e.selectedClusterBoundaryFeature)}))).apply(this,arguments)}function m(e){return F.apply(this,arguments)}function F(){return(F=t._asyncToGenerator((function*(e){const{selectedFeature:t,view:r}=e,o=t.sourceLayer,i=yield r.whenLayerView(o),s=i.createQuery();s.aggregateIds=[t.getObjectId()],n.browseClusteredFeatures.active=!0,n.browseClusteredFeatures.disabled=!0;const{features:a}=yield i.queryFeatures(s);n.browseClusteredFeatures.active=!1,n.browseClusteredFeatures.disabled=!1,r.popup.open({features:[t].concat(a),featureMenuOpen:!0})}))).apply(this,arguments)}function h(e){const t=e.features.filter((e=>l(e)));t.length&&(e.features=t)}e.browseAggregateFeatures=m,e.displayClusterExtent=f,e.getSelectedTarget=c,e.isClusterFeature=l,e.isZoomScreenSize=d,e.removeClusteredFeaturesForBrowsing=h,e.triggerAction=u,e.zoomToClusterExtent=w,e.zoomToLocation=g,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
