const t=window.webmapgl.h;class e{constructor(){this.count=0,this.liked=!1,this.attribute="_likes",this.disabled=!1,this.iconNo="star-outline",this.iconYes="star",this.method="POST"}async componentWillLoad(){await this.setState()}async setState(){this.feature&&(this.count=this.feature.properties[this.attribute],this.proxy=await this.likeCtrl.create(this.feature),this.liked=this.proxy.isLiked())}async toggle(){this.liked=!this.liked,this.count+=this.liked?1:-1,this.feature&&this.feature.properties&&(this.feature.properties[this.attribute]=this.count),this.liked?this.proxy.like():this.proxy.unlike();let t=!1,e=this.proxy.getClientId();try{await this.restCtrl.create({type:"Feature",properties:{action:this.liked?"like":"unlike",feature_id:this.feature.id,client_id:e}},{url:this.url,token:this.token,method:this.method,mode:this.requestMode}),t=!0}catch(t){}this.glLike.emit({success:t,feature:this.feature,action:this.liked?"like":"unlike",client_id:e})}render(){return t("ion-button",{fill:"clear",onClick:()=>{this.disabled||this.toggle()},disabled:this.disabled},t("ion-icon",{slot:"start",name:this.liked?this.iconYes:this.iconNo}),t("span",{class:"gl-like-button-count"},this.count))}static get is(){return"gl-like-button"}static get properties(){return{attribute:{type:String,attr:"attribute"},count:{state:!0},disabled:{type:Boolean,attr:"disabled"},el:{elementRef:!0},feature:{type:"Any",attr:"feature",mutable:!0,watchCallbacks:["setState"]},iconNo:{type:String,attr:"icon-no"},iconYes:{type:String,attr:"icon-yes"},likeCtrl:{connect:"gl-like-controller"},liked:{state:!0},method:{type:String,attr:"method"},proxy:{state:!0},requestMode:{type:String,attr:"request-mode"},restCtrl:{connect:"gl-rest-controller"},token:{type:String,attr:"token"},url:{type:String,attr:"url"}}}static get events(){return[{name:"glLike",method:"glLike",bubbles:!0,cancelable:!0,composed:!0}]}}class i{constructor(t,e={}){this.feature=t,e.keyPrefix||(e.keyPrefix="webmapgl.like"),e.clientId||(e.clientId=localStorage.getItem(e.keyPrefix+".clientId"),e.clientId||(e.clientId=this.makeId(64),localStorage.setItem(e.keyPrefix+".clientId",e.clientId))),this.options=e}getClientId(){return this.options.clientId}isLiked(){return-1!==this.getArray(this.feature.layer).indexOf(this.feature.id)}like(){let t=this.getArray(this.feature.layer);-1===t.indexOf(this.feature.id)&&t.push(this.feature.id),this.setArray(this.feature.layer,t)}unlike(){let t=this.getArray(this.feature.layer),e=t.indexOf(this.feature.id);-1!==e&&t.splice(e,1),this.setArray(this.feature.layer,t)}makeId(t){var e="",i="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";for(let r=0;r<t;r++)e+=i.charAt(Math.floor(Math.random()*i.length));return e}getLayerKey(t){return this.options.keyPrefix+"|"+document.location.pathname+"|"+(t||"default")}getArray(t){let e=localStorage.getItem(this.getLayerKey(t));return e?e.split(",").map(t=>parseInt(t)).filter(t=>!isNaN(t)):[]}setArray(t,e){let i=e.filter(t=>!isNaN(t)).map(t=>t.toString()).join(",");localStorage.setItem(this.getLayerKey(t),i)}}class r{async create(t,e={}){return new i(t,e)}static get is(){return"gl-like-controller"}static get properties(){return{create:{method:!0}}}}class s{async create(t,e){let i={"Content-Type":"application/json"};return e.token&&(i.Authorization="Bearer "+e.token),await fetch(e.url,{body:JSON.stringify(t),headers:i,method:e.method||"POST",mode:e.mode||"cors"})}static get is(){return"gl-rest-controller"}static get properties(){return{create:{method:!0}}}}export{e as GlLikeButton,r as GlLikeController,s as GlRestController};