/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{equals as t}from"../../core/arrayUtils.js";import{isMultiOriginJSONMixin as o}from"../../core/multiOriginJSONSupportUtils.js";import{project as n}from"../../geometry/projectionUtils.js";import r from"../../geometry/SpatialReference.js";import{fromValues as e,containsXY as i}from"../../geometry/support/aaBoundingRect.js";import s from"./AttributionItem.js";const c=10;class a{constructor(t,o,n){this.index=t,this.bbox=o,this.score=n}}function u(t){const o=t.contributors,n={maxZoom:0,attributions:[]};if(!o)return n;for(const r of o){if(!r.coverageAreas?.length||!r.attribution)continue;const t=n.attributions.length;n.attributions.push(r.attribution);for(const o of r.coverageAreas){const r=o.bbox,i=new a(t,e(r[1],r[0],r[3],r[2]),o.score??100);for(let t=o.zoomMin;t<=o.zoomMax;t++)n[t]??=[],n[t].push(i)}}return n.maxZoom=Math.max.apply(null,Object.keys(n).map(t=>+t).filter(t=>Number.isFinite(t))),n}function l(t,o,e,c){let a=o?.scaleToZoom(e)??0;if(a=Math.min(t.maxZoom??0,Math.round(a)),!c||null==a||a<=-1)return[];const u=t[a],{x:l,y:f}=n(c.center.clone().normalize(),r.WGS84),m=new Set;return u?.filter(t=>{const o=!m.has(t.index)&&i(t.bbox,l,f);return o&&m.add(t.index),o}).map(o=>new s(t.attributions[o.index],o.score))??[]}function f(t){return p(t,"user")?new s(t.copyright,c):null}function m(t){return p(t)?new s(t.copyright,c):null}function p(t,n){return null!=t&&"object"==typeof t&&"copyright"in t&&"string"==typeof t.copyright&&t.copyright.length>0&&(!n||o(t)&&"user"===t.originOf("copyright"))}function b(t){return null!=t&&"object"==typeof t&&"hasAttributionData"in t&&!0===t.hasAttributionData&&"attributionDataUrl"in t&&"fetchAttributionData"in t&&"function"==typeof t.fetchAttributionData}function h(t){return null!=t&&"object"==typeof t&&"portalItem"in t&&null!=t.portalItem&&"object"==typeof t.portalItem&&"accessInformation"in t.portalItem&&"string"==typeof t.portalItem.accessInformation&&t.portalItem.accessInformation.length>0}function g(t){return h(t)?new s(t.portalItem.accessInformation,c):null}function y(o,n){const r=new globalThis.Map;for(const t of n){const o=r.get(t.text);o?t.score>o.score&&r.set(t.text,t):r.set(t.text,t)}const e=Array.from(r.values()).sort((t,o)=>o.score-t.score);return o&&t(o,e,(t,o)=>t.text===o.text)?o:e}export{u as createAttributionIndex,y as deduplicateAttributionItems,g as getAttributionItemFromAccessInformation,m as getAttributionItemFromLayerCopyright,f as getAttributionItemFromUserCopyright,l as getAttributionItemsFromIndex,b as isDynamicAttributionLayer};
