(()=>{"use strict";window.addEventListener("load",function(){!function(){const e="#commentform",t="sp-upload-review-image";jQuery(function(a){a(e).attr("enctype","multipart/form-data"),a("a.sp-upvote-button").each((e,t)=>{const o=a(t).data("review-id"),n=a(t).data("key");"voted"===localStorage.getItem("nv_apr_upvote_"+n+o)&&(a(t).attr("data-already-liked",1),a(t).find(".sp-upvote-icon").addClass("active"))});let o=!1;a(".sp-apr-upvote-section").on("click","a.sp-upvote-button",e=>{if(e.preventDefault(),!0===o)return;const t=a(e.currentTarget),n="1"===t.attr("data-already-liked"),l=t.data("review-id"),s=t.data("product-id"),r=t.data("key");let d=localStorage.getItem("nv_apr_upvote_"+r+l);n&&"voted"!==d&&(localStorage.setItem("nv_apr_upvote_"+r+l,"voted"),d="voted"),a.ajax({type:"post",url:sparksApr.adminAjaxUrl+s+"/vote",beforeSend(e){o=!0,e.setRequestHeader("X-WP-Nonce",sparksApr.adminAjaxNonce)},data:{reviewId:l,hasVoted:d},success(e){if(o=!1,e.success){const a=e.latestCount;t.find(".sp-upvote-count").html(a>0?a:""),t.attr("data-already-liked",n?0:1),n?localStorage.removeItem("nv_apr_upvote_"+r+l):localStorage.setItem("nv_apr_upvote_"+r+l,"voted"),t.find(".sp-upvote-icon").toggleClass("active")}}})}),a(".sp-review-gallery").on("click","a.sp-review-image",e=>{e.preventDefault();const t="review-image-modal",a=e.currentTarget.getAttribute("data-image"),o=document.querySelector(`#${t}`),n=document.querySelector(`#${t} .sp-modal-container`),l=document.querySelector(`#${t} .sp-modal-container .sp-modal-inner-content`),s=document.createElement("img");s.style.display="block",s.style.width="100%",s.src=a,o.classList.add("open"),n.style.height="auto",n.classList.remove("is-loading"),l.innerHTML=s.outerHTML});const n=[],l=e=>{document.getElementById("sp-upload-max").style.display="none";const t=document.getElementById("sp-upload-review-list"),a=function(e,t){const a=new FileReader;a.readAsDataURL(e[t]),a.onload=function(e){document.getElementById("img_preview_"+t).style.backgroundImage='url("'+e.target.result+'")'}};for(;t.hasChildNodes();)t.removeChild(t.firstChild);for(let o=0;o<e.files.length;o++){const n=document.createElement("li");n.classList.add("sp-upload-img-preview"),n.setAttribute("id","img_preview_"+o),n.innerHTML='<button value="'+o+'" class="sp-apr-remove-product">×</button>',a(e.files,o),t.appendChild(n)}};a("#sp-do-upload").click(function(){a(`#${t}`).click()}),a(`#${t}`).on("change",e=>{if(!e.target.files)return;const a=e.target.files,o=Array.from(a);if(n.length+o.length>5){document.getElementById("sp-upload-max").style.display="block";const e=new DataTransfer;for(let t=0;t<n.length;t++){const a=n[t];e.items.add(a)}return void(document.getElementById(t).files=e.files)}const s=new DataTransfer;for(let e=0;e<n.length;e++){const t=n[e];s.items.add(t)}for(let e=0;e<o.length;e++){const t=a[e];s.items.add(t),n.push(t)}const r=document.getElementById(t);r.files=s.files,l(r)}),a("#sp-upload-review-list").on("click","li .sp-apr-remove-product",e=>{e.preventDefault();const a=parseInt(e.target.value),o=document.getElementById(t),s=new DataTransfer,{files:r}=o;for(let e=0;e<r.length;e++){const t=r[e];a===e&&n.splice(e,1),a!==e&&s.items.add(t)}o.files=s.files,l(o)}),a(e).submit(function(){if(document.getElementById(t).files.length>5)return!1})})}()})})();