(()=>{class t{constructor(t){this.wrapper=t,this.expirationDateWrapper=this.wrapper.querySelector(".wpcd-coupon-expiration-date"),this.copyButton=this.wrapper.querySelector(".wpcd-coupon-button:not(.wpcd-popup-button)"),this.showCodeButton=this.wrapper.querySelector(".wpcd-coupon-button.wpcd-popup-button"),this.expirationDate=new Date(this.wrapper.dataset.expiration_date).getTime(),this.expiredDateText=this.wrapper.dataset.expired_date_text,this.couponCode=this.wrapper.dataset.coupon_code,this.init()}init(){this.checkExpirationDate(),this.setupCopyButton(),this.handleHiddenCoupon(),this.handleTemplateTwoExpirationDate();const t=new URL(location),e=t?.searchParams?.get("wpcd_coupon");this.wrapper.classList.contains("wpcd-coupon-hidden")&&this.showCodeButton&&!e&&this.handleShowCode()}handleTemplateTwoExpirationDate(){if(!this.wrapper.classList.contains("wpcd-coupon-template-two")&&!this.wrapper.classList.contains("wpcd-coupon-template-seven")&&!this.wrapper.classList.contains("wpcd-coupon-template-six"))return;if(this.expirationDateWrapper.classList.contains("wpcd-coupon-does-not-expire"))return;const t=this.expirationDateWrapper.querySelectorAll("span")[1];if(!t)return;const e=()=>{const e=(new Date).getTime(),o=this.expirationDate-e;if(o<0)return void(t.textContent=this.expiredDateText);const n=Math.floor(o/6048e5),p=Math.floor(o%6048e5/864e5),i=Math.floor(o%864e5/36e5),a=Math.floor(o%36e5/6e4),s=Math.floor(o%6e4/1e3);t.textContent=`${n} weeks ${p} days ${i} hours ${a} minutes ${s} seconds`};e(),setInterval(e,1e3)}handleShowCode(){this.showCodeButton.addEventListener("click",(()=>{const t=this.wrapper.getAttribute("data-coupon_id"),e=new URL(location);e.searchParams.set("wpcd_coupon",t),window.open(e,"_blank")}))}handleHiddenCoupon(){if(!this.wrapper.classList.contains("wpcd-coupon-hidden"))return;const t=new URL(location),e=t?.searchParams?.get("wpcd_coupon");if(!e||"null"===e)return;const o=document.getElementById(e),n=this.wrapper.querySelector(".wpcd-coupon-button.wpcd-popup-button");n.removeAttribute("href"),n.classList.add("wpcd-coupon-popup-opened"),o.style.display="block",o.querySelector(".wpcd-coupon-popup-close-button").addEventListener("click",(()=>{o.style.display="none"}))}checkExpirationDate(){this.expirationDateWrapper.classList.contains("wpcd-coupon-does-not-expire")||Date.now()>this.expirationDate&&(this.expirationDateWrapper.textContent=this.expiredDateText,this.expirationDateWrapper.classList.add("wpcd-coupon-expired"))}setupCopyButton(){if(!this.copyButton)return;const t=this.copyButton.innerText;this.copyButton.addEventListener("click",(()=>{this.copyButton.innerText="Copied!",this.copyToClipboard(),setTimeout((()=>{this.copyButton.innerText=t.trim()}),1e3)}))}async copyToClipboard(){const t=document.createElement("input");t.value=this.couponCode,t.type="text",document.body.appendChild(t),t.select(),document.execCommand("Copy"),document.body.removeChild(t)}}window.addEventListener("DOMContentLoaded",(()=>{document.querySelectorAll(".wpcd-coupon-wrapper:not(.wpcd-coupon-type-deal)").forEach((e=>{new t(e)}))}))})();