/*! api.rpkg.net | (c) Written by Obi Obianom, www.obianom.com | all rights reserved 
             _                        _   
  _ _  _ __ | |__ __ _     _ _   ___ | |_ 
 | '_|| '_ \| / // _` | _ | ' \ / -_)|  _|
 |_|  | .__/|_\_\\__, |(_)|_||_|\___| \__|
      |_|        |___/                    

*/
document.addEventListener("DOMContentLoaded",function(){Shiny.addCustomMessageHandler("triggerViewsLikesFollows",function(e){likebuttonid=e.likes,followbuttonid=e.follows,viewsbuttonid=e.views,null!=e.follows&&(fetchFollowing(api),ewl("#"+followbuttonid).then(e=>{e.addEventListener(clik,function(){toggleFollow(api)})})),null!=e.views&&updateViews(api,e.views),null!=e.likes&&(fetchLike(api),ewl("#"+likebuttonid).then(e=>{e.addEventListener(clik,function(){toggleLike(api)})}))})});const eapi=document.getElementById("rpkgapiscript"),api=eapi.getAttribute("identifier"),likeic="Like",likeib="♥",followc="Follow",followb="★",clik="click";var apiendpoint="https://api.rpkg.net/endp/",viewsbuttonid="views",liked00x020=!1,liked00x040=!1,likebuttonid="button1",liketext=document.querySelector(likebuttonid+" .te1x1tres"),likecount=document.querySelector(likebuttonid+" .tex1tc1ount"),followd00x020=!1,followd00x040=!1,followbuttonid="follow1",followtext=document.querySelector(followbuttonid+" .te1x1tres"),followcount=document.querySelector(followbuttonid+" .tex1tc1ount"),viewsname=" views",viewscount=0,methd="GET",followerscount=0,commonerr="An error occurred, possibly because of wrong API key or configurations. Go to https://api.rpkg.net/endp/help to learn more or open an issue with us at https://github.com/oobianom/rpkg.api/issues",fchgc=vchgc="exc";function prepLikeButton(e,t=null){"exc"==vchgc&&(vchgc=e);let o=!0==vchgc?viewscount-1:viewscount+1;$("#"+likebuttonid+"-totalv2").html($("#"+likebuttonid+"-totalv2").html().toString()===viewscount.toString()?o:viewscount),Shiny.setInputValue("feedliketracker",{liked:e,total:viewscount})}function prepFollowButton(e,t=null){"exc"==fchgc&&(fchgc=e);let o=!0==fchgc?followerscount-1:followerscount+1;$("#"+followbuttonid+"-totalv2").html($("#"+followbuttonid+"-totalv2").html().toString()===followerscount.toString()?o:followerscount),Shiny.setInputValue("feedfollowtracker",{followed:e,total:followerscount})}function updateViewId(e,t){$("#"+viewsbuttonid).html(e),Shiny.setInputValue("totalviewstracker",e)}function toggleLike(e){try{let t=new XMLHttpRequest;t.open(methd,apiendpoint+"likeornot/"+e+"?type=ssl"),t.send(),t.responseType="json",t.onload=()=>{if(4==t.readyState&&200==t.status){let e=t.response;if(5==e.detached)return alert(commonerr),!1;window.liked00x020=Boolean(e.attached[0]),prepLikeButton(window.liked00x020)}else console.log(`Error: ${t.status}`)}}catch(o){console.log("The following error has occurred due to misconfigurations"),console.log(o.message)}}function toggleFollow(e){try{let t=new XMLHttpRequest;t.open(methd,apiendpoint+"followmeornot/"+e+"?type=ssl"),t.send(),t.responseType="json",t.onload=()=>{if(4==t.readyState&&200==t.status){let e=t.response;if(5==e.detached)return alert(commonerr),!1;window.followd00x020=Boolean(e.attached[0]),prepFollowButton(window.followd00x020)}else console.log(`Error: ${t.status}`)}}catch(o){console.log("The following error has occurred due to misconfigurations"),console.log(o.message)}}function fetchLike(e){try{let t=new XMLHttpRequest;t.open(methd,apiendpoint+"fetchlikes/"+e+"?type=ssl"),t.send(),t.responseType="json",t.onload=()=>{if(4==t.readyState&&200==t.status){let e=t.response;if(5==e.detached)return alert(commonerr),!1;window.viewscount=e.attached[0],window.liked00x040=Boolean(e.liked[0]),prepLikeButton(liked00x040,viewscount)}else console.log(`Error: ${t.status}`)}}catch(o){console.log("The following error has occurred due to misconfigurations"),console.log(o.message)}return viewscount}function fetchFollowing(e){try{let t=new XMLHttpRequest;t.open(methd,apiendpoint+"fetchfollow/"+e+"?type=ssl"),t.send(),t.responseType="json",t.onload=()=>{if(4==t.readyState&&200==t.status){let e=t.response;if(5==e.detached)return alert(commonerr),!1;window.followerscount=e.attached[0],window.followd00x040=Boolean(e.liked[0]),prepFollowButton(followd00x040,followerscount)}else console.log(`Error: ${t.status}`)}}catch(o){console.log("The following error has occurred due to misconfigurations"),console.log(o.message)}return followerscount}function updateViews(e,t){let o=new XMLHttpRequest;o.open(methd,apiendpoint+"updateviews/"+e+"?type=ssl"),o.send(),o.responseType="json",o.onload=()=>{if(4==o.readyState&&200==o.status){let e=o.response;if(5==e.detached)return alert(commonerr),!1;updateViewId(e.attached[0]+viewsname,t)}else console.log(`Error: ${o.status}`)}}function ewl(e){return new Promise(t=>{if(document.querySelector(e))return t(document.querySelector(e));let o=new MutationObserver(l=>{document.querySelector(e)&&(t(document.querySelector(e)),o.disconnect())});o.observe(document.body,{childList:!0,subtree:!0})})}
