/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{__decorate as e}from"tslib";import t from"../core/Accessor.js";import{makeHandle as s}from"../core/handleUtils.js";import{isActivationKey as i}from"../core/keyboard.js";import{watch as o,initial as r}from"../core/reactiveUtils.js";import{renderingSanitizer as n}from"../core/sanitizerUtils.js";import{addFrameTask as a}from"../core/scheduling.js";import{property as h,subclass as c}from"../core/accessorSupport/decorators.js";import{getCalciteModeClassName as d,setCalciteModeClass as l}from"../support/modeUtils.js";import{globalCss as p}from"../widgets/support/globalCss.js";const u="esri-attribution",_={poweredBy:`${u}__powered-by`,sources:`${u}__sources`,sourcesOpen:`${u}__sources--open`,link:`${u}__link`};let m=class extends t{constructor(e){super(e),this._previousSourceHeight=0,this._opened=!1,this._overflowing=!1,this._changeSet=new Set,this.height=0,this._toggleState=()=>{(this._opened||this._overflowing)&&(this._opened=!this._opened,this._invalidate("opened"))},this.surface=document.createElement("div"),this._task=a({render:()=>{this._update(),this._task.pause(),this._changeSet.clear()}}),this.addHandles(this._task)}appendBefore(e,t){e.insertBefore(this.surface,t),this._invalidate("mode")}initialize(){const e=document.createElement("div");e.setAttribute("dir","ltr"),e.classList.add("esri-widget","esri-view-attribution","esri-attribution"),this._set("surface",e);const t=new ResizeObserver(()=>{this.height=e.clientHeight});t.observe(e),this.addHandles(s(()=>t.disconnect())),e.innerHTML=`\n      <div class="${_.sources}"></div>\n      <div class="${_.poweredBy}">\n        Powered by\n        <a class="${_.link}" href="https://www.esri.com/" rel="noreferrer" target="_blank">Esri</a>\n      </div>`,this.addHandles([this.surface,o(()=>this.attributionItems,()=>this._invalidate("attributionItems"),r),o(()=>this.mode,()=>this._invalidate("mode"),r)]);const n=e.querySelector(`.${_.sources}`);this._previousSourceHeight=n.clientHeight;const a=new ResizeObserver(()=>{this._invalidate("overflow")});a.observe(n),this.addHandles(s(()=>a.disconnect()));const h=new MutationObserver(()=>{this._invalidate("overflow")});h.observe(n,{childList:!0,subtree:!0,characterData:!0}),this.addHandles(s(()=>h.disconnect())),n.onclick=this._toggleState,n.onkeydown=e=>{i(e.key)&&(e.preventDefault(),this._toggleState())}}_invalidate(e){this._changeSet.add(e),this._task.resume()}_update(){const e=this.surface.querySelector(`.${_.sources}`);if(this._changeSet.has("overflow")){const{clientHeight:t,clientWidth:s,scrollWidth:i}=e,o=t<this._previousSourceHeight;this._previousSourceHeight=t,this._opened?o&&(this._opened=!1):o||(this._overflowing=i>s)}if(this._changeSet.has("overflow")||this._changeSet.has("opened")){const t=this._opened||this._overflowing,s=t?"0":"";e.setAttribute("tabindex",s),e.classList.toggle(p.interactive,t),e.classList.toggle(_.sourcesOpen,this._opened)}this._changeSet.has("attributionItems")&&(e.innerHTML=n.sanitize(this.attributionItems?.map(e=>e.text).join(" | ")??"")),this._changeSet.has("mode")&&(this.surface.classList.remove(d("light"),d("dark")),this.mode?this.surface.classList.add(d(this.mode)):l(this.surface))}};e([h()],m.prototype,"attributionItems",void 0),e([h()],m.prototype,"height",void 0),e([h({type:["dark","light"]})],m.prototype,"mode",void 0),e([h({readOnly:!0})],m.prototype,"surface",void 0),m=e([c("esri.views.Attribution")],m);export{m as Attribution};
