/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["../../chunks/_rollupPluginBabelHelpers","../../chunks/tslib.es6","../../geometry","../../core/asyncUtils","../../core/Collection","../../core/HandleOwner","../../core/reactiveUtils","../../core/accessorSupport/decorators/property","../../core/arrayUtils","../../core/has","../../core/accessorSupport/ensureType","../../core/accessorSupport/decorators/subclass","../../geometry/support/contains","../../geometry/support/webMercatorUtils","../../geometry/SpatialReference"],(function(t,e,i,n,r,o,a,s,u,c,l,d,h,p,y){"use strict";function f(t,e){return t&&"copyright"in t&&(!e||"function"==typeof t.originOf&&"user"===t.originOf("copyright"))}function b(t,e){return t.length!==e.length||t.some(((t,i)=>t.text!==e[i].text))}function g(t,e,i){if(!i||!e)return;t.find((t=>t.layerView===e&&t.text===i))||t.push({text:i,layerView:e})}function _(t){return"bing-maps"===t.type}const m=[];let v=function(e){function i(t){var i;return(i=e.call(this,t)||this)._clear=()=>{i._fetchedAttributionData.clear(),i._pendingAttributions.clear(),i.handles.remove("suspension"),i.notifyChange("state")},i._pendingAttributions=new Set,i._fetchedAttributionData=new Map,i.items=new r,i.view=null,i._allLayerViewsChange=t=>{i.handles.remove("suspension");const e=i.get("view.allLayerViews");e&&i.handles.add(e.map((t=>a.watch((()=>{var e;return[t.suspended,null==(e=t.layer)?void 0:e.attributionVisible]}),(()=>i._updateAttributionItems())))),"suspension"),t&&t.removed&&t.removed.forEach((t=>{i._pendingAttributions.delete(t),i._fetchedAttributionData.delete(t)})),i._updateAttributionItems()},i.handles.add([a.on((()=>{var t;return null==(t=i.view)?void 0:t.allLayerViews}),"change",(t=>i._allLayerViewsChange(t)),{onListenerAdd:()=>i._allLayerViewsChange(),onListenerRemove:i._clear}),a.when((()=>{var t;return!0===(null==(t=i.view)?void 0:t.stationary)}),(()=>i._updateAttributionItems()))]),i}t._inheritsLoose(i,e);var o=i.prototype;return o.destroy=function(){this.view=null,this._fetchedAttributionData.clear(),this._pendingAttributions.clear(),this.items.removeAll()},o._updateAttributionItems=function(){const t=this.get("view.allLayerViews");m.length=0,t?(t.forEach((t=>{var e;if(t.suspended||null==(e=t.layer)||!e.attributionVisible)return;const i=t.layer;if(f(i,"user"))return void g(m,t,i.copyright);if(i.hasAttributionData){if(this._fetchedAttributionData.has(t)){const e=this._fetchedAttributionData.get(t);return void(e?g(m,t,this._getDynamicAttribution(e,this.view,i)):f(i)&&g(m,t,i.copyright))}return void this._fetchAttributionData(t)}const n=i.get("portalItem.accessInformation");g(m,t,n||i.copyright)})),b(this.items,m)&&(this.items.removeAll(),this.items.addMany(m)),m.length=0,this.notifyChange("state")):this._clear()},o._fetchAttributionData=function(){var e=t._asyncToGenerator((function*(t){if(this._pendingAttributions.has(t))return;this._pendingAttributions.add(t);const e=yield n.result(t.layer.fetchAttributionData());if(this._pendingAttributions.has(t)){const i=e.ok?this._createContributionIndex(e.value,_(t.layer)):null;this._pendingAttributions.delete(t),this._fetchedAttributionData.set(t,i)}this._updateAttributionItems()}));function i(t){return e.apply(this,arguments)}return i}(),o._createContributionIndex=function(t,e){const i=t.contributors,n={};if(!i)return n;for(let r=0;r<i.length;r++){const t=i[r],o=t.coverageAreas;if(!o)return;for(const i of o){const o=i.bbox,a=i.zoomMin-(e&&i.zoomMin?1:0),s=i.zoomMax-(e&&i.zoomMax?1:0),u={xmin:o[1],ymin:o[0],xmax:o[3],ymax:o[2],spatialReference:y.WGS84},c={extent:p.geographicToWebMercator(u),attribution:t.attribution||"",score:null!=i.score?i.score:100,id:r};for(let t=a;t<=s;t++)n[t]=n[t]||[],n[t].push(c)}}return n.maxKey=Math.max.apply(null,Object.keys(n)),n},o._getDynamicAttribution=function(t,e,i){const{extent:n,scale:r}=e;let o=i.tileInfo.scaleToZoom(r);if(o=Math.min(t.maxKey,Math.round(o)),!n||null==o||o<=-1)return"";const a=t[o],s=p.project(n.center.clone().normalize(),e.spatialReference),u={};return a?a.filter((t=>{const e=!u[t.id]&&s&&h.extentContainsPoint(t.extent,s);return e&&(u[t.id]=!0),e})).sort(((t,e)=>e.score-t.score||t.objectId-e.objectId)).map((t=>t.attribution)).join(", "):""},t._createClass(i,[{key:"state",get:function(){return this.get("view.ready")?this._pendingAttributions.size>0?"loading":"ready":"disabled"}}]),i}(o.HandleOwner);e.__decorate([s.property({readOnly:!0,type:r})],v.prototype,"items",void 0),e.__decorate([s.property({readOnly:!0})],v.prototype,"state",null),e.__decorate([s.property()],v.prototype,"view",void 0),v=e.__decorate([d.subclass("esri.widgets.Attribution.AttributionViewModel")],v);return v}));
