"use strict";class AIOVGLikeButtonElement extends HTMLElement{constructor(){super(),this._likeButtonEl=null,this._dislikeButtonEl=null,this._isRendered=!1,this._isLoading=!1,this._options={},this._params={}}connectedCallback(){return!this._isRendered&&0!=this.postId&&(this._options=window.aiovg_likes,(!!this.hasLikeButton||!!this.hasDislikeButton)&&void(this._isRendered=!0,this._params={likes:parseInt(this.getAttribute("likes")||0),dislikes:parseInt(this.getAttribute("dislikes")||0),liked:this.hasAttribute("liked"),disliked:this.hasAttribute("disliked")},this.hasLikeButton&&(this._likeButtonEl=document.createElement("button"),this._likeButtonEl.type="button",this._likeButtonEl.className="aiovg-button-like",this._updateLikeButton(),this.appendChild(this._likeButtonEl),this._likeButtonEl.addEventListener("click",t=>this._toggleLikes(t))),this.hasDislikeButton&&(this._dislikeButtonEl=document.createElement("button"),this._dislikeButtonEl.type="button",this._dislikeButtonEl.className="aiovg-button-dislike",this._updateDislikeButton(),this.appendChild(this._dislikeButtonEl),this._dislikeButtonEl.addEventListener("click",t=>this._toggleDislikes(t))),this._load()))}disconnectedCallback(){this._likeButtonEl&&this._likeButtonEl.removeEventListener("click",t=>this._toggleLikes(t)),this._dislikeButtonEl&&this._dislikeButtonEl.removeEventListener("click",t=>this._toggleDislikes(t))}get postId(){return parseInt(this.getAttribute("post_id")||0)}get hasLikeButton(){return parseInt(this._options.show_like_button)||0}get hasDislikeButton(){return parseInt(this._options.show_dislike_button)||0}get userId(){return parseInt(this._options.user_id)}get loginRequired(){return parseInt(this._options.login_required_to_vote)||0}get isLoaded(){return this.hasAttribute("loaded")}set isLoaded(t){return this.setAttribute("loaded",t)}_load(){if(this.isLoaded)return!1;let t={action:"aiovg_get_likes_dislikes_info",user_id:this.userId,post_id:this.postId,security:this._options.ajax_nonce};this._fetch(t,t=>{this.isLoaded=!0,"success"==t.status&&(this._params=t,this._updateLikeButton(),this._updateDislikeButton())})}_toggleLikes(){if(this.loginRequired&&0===this.userId)return alert(this._options.i18n.alert_login_required),!1;if(this._isLoading)return!1;let t={action:"aiovg_toggle_likes",user_id:this.userId,post_id:this.postId,context:this._params.liked?"remove_from_likes":"add_to_likes",security:this._options.ajax_nonce};this._isLoading=!0,this._likeButtonEl.querySelector("svg").classList.add("aiovg-animate-rotate"),this._fetch(t,t=>{this._isLoading=!1,"success"==t.status&&(this._params=t),this._updateLikeButton(),this._updateDislikeButton()})}_toggleDislikes(){if(this.loginRequired&&0===this.userId)return alert(this._options.i18n.alert_login_required),!1;if(this._isLoading)return!1;let t={action:"aiovg_toggle_dislikes",user_id:this.userId,post_id:this.postId,context:this._params.disliked?"remove_from_dislikes":"add_to_dislikes",security:this._options.ajax_nonce};this._isLoading=!0,this._dislikeButtonEl.querySelector("svg").classList.add("aiovg-animate-rotate"),this._fetch(t,t=>{this._isLoading=!1,"success"==t.status&&(this._params=t),this._updateLikeButton(),this._updateDislikeButton()})}_updateLikeButton(){if(!this._likeButtonEl)return!1;let t="";this._params.liked?t+='':t+='',t+='',t+=''+this._params.likes+"",t+=''+this._options.i18n.likes+"",t+="",this._likeButtonEl.innerHTML=t}_updateDislikeButton(){if(!this._dislikeButtonEl)return!1;let t="";this._params.disliked?t+='':t+='',t+='',t+=''+this._params.dislikes+"",t+=''+this._options.i18n.dislikes+"",t+="",this._dislikeButtonEl.innerHTML=t}_fetch(t,i){jQuery.post(this._options.ajax_url,t,i,"json")}}document.addEventListener("DOMContentLoaded",function(){customElements.define("aiovg-like-button",AIOVGLikeButtonElement)});