import*as e from"@wordpress/interactivity";import*as t from"@surecart/api-fetch";var r,o,n={d:function(e,t){for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},o:function(e,t){return Object.prototype.hasOwnProperty.call(e,t)}},a=(r={getContext:function(){return e.getContext},getElement:function(){return e.getElement},store:function(){return e.store}},o={},n.d(o,r),o),s=function(e){var t={};return n.d(t,e),t}({default:function(){return t.default}});let i=[];function c(e){return e.classList.contains("a11y-speak-region")||e.classList.contains("a11y-speak-intro-text")||e.querySelector("#a11y-speak-assertive, #a11y-speak-polite, #a11y-speak-intro-text")}const{__}=wp.i18n,{addQueryArgs:u}=wp.url,d=e=>!e?.key||[" ","Enter","Escape"].includes(e.key),l=()=>{requestAnimationFrame((()=>{const e=document.querySelector(".wp-block-surecart-product-review-form .sc-product-review-form-dialog"),t=e?.querySelector('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');t?.focus()}))},{state:p,actions:m,callbacks:f}=(0,a.store)("surecart/product-review-form",{actions:{*open(e){if(!d(e))return;e?.preventDefault();const{redirect_url:t,product_id:r}=(0,a.getContext)();if(p.openButton=e?.target?.closest(".wp-block-surecart-product-review-add-button"),t)return void(window.location.href=t);(e=>{const t=new URL(window.location.href);t.searchParams.set("product-review-form",e),window.history.pushState({},"",t.toString())})(r),p.open=!0;const{speak:o}=yield import("@wordpress/a11y");o(__("Product review form opened.","surecart"),"assertive"),l()},*close(e){if(!p.open||!d(e))return;const t=(0,a.getContext)();if(e?.preventDefault(),(e=>{const{stars:t,title:r,body:o}=e;return t>0||r&&""!==r.trim()||o&&""!==o.trim()})(t)&&!window.confirm(__("Discard your review?","surecart")))return;f.clearForm(),p.open=!1,t.submitted=!1,(()=>{const e=new URL(window.location.href);e.searchParams.delete("product-review-form"),window.history.pushState({},"",e.toString())})();const{speak:r}=yield import("@wordpress/a11y");r(__("Product review form closed.","surecart"),"assertive"),setTimeout((()=>p?.openButton?.focus()),1)},setStars(e){(0,a.getContext)().stars=e?.target?.value??0},handleStarKeydown(e){" "!==e.key&&"Enter"!==e.key||(e.preventDefault(),m.selectStar(e.target))},handleStarClick(e){m.selectStar(e.currentTarget)},selectStar(e){const t=e.getAttribute("data-star"),r=document.getElementById(`stars-star${t}`);r&&(r.checked=!0,r.dispatchEvent(new Event("change",{bubbles:!0})))},setTitle(e){(0,a.getContext)().title=e?.target?.value??""},setContent(e){(0,a.getContext)().body=e?.target?.value??""},*handleSubmit(e){if("keydown"===e.type&&"Enter"!==e.key)return!0;if(e.preventDefault(),e.stopPropagation(),!e.submitter){const{ref:t}=(0,a.getElement)();t&&(e.submitter=t)}if(!e?.submitter?.value){const e=(0,a.getContext)(),{stars:t,title:r,sc_product_id:o,body:n}=e,{speak:i}=yield import("@wordpress/a11y");if((e=>{const{stars:t,title:r,sc_product_id:o}=e;return!t||t<1||t>5||!r||""===r?.trim()||!o})(e))return;try{e.busy=!0,i(__("Submitting your review.","surecart"),"assertive");const a=yield(0,s.default)({method:"POST",path:u("/surecart/v1/reviews"),data:{product:o,stars:t,title:r,body:n}});if(!a||!a?.id)throw new Error(__("Failed to submit review, please try again.","surecart"));f.clearForm(),e.submitted=!0,requestAnimationFrame((()=>{const e=document.querySelector(".sc-product-review-confirmation"),t=e?.querySelector('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])');t?t.focus():e&&(e.setAttribute("tabindex","-1"),e.focus())}))}catch(e){console.error(e);let t=e?.message||__("Failed to submit review. Please try again.","surecart");e?.additional_errors&&Array.isArray(e.additional_errors)&&(t+=" "+e.additional_errors.map((e=>e.message)).join(" ")),alert(t)}finally{e.busy=!1}}}},callbacks:{handleInit(){p.open&&l()},handleOpenChange(){p.open?(document.body.classList.add("sc-product-review-form-open"),function(e){i=[];let t=e;for(;t&&t!==document.body;){const e=t.parentElement;if(!e)break;Array.from(e.children).forEach((e=>{e===t||e.hasAttribute("inert")||c(e)||(e.setAttribute("inert",""),i.push(e))})),t=e}}(document.querySelector(".wp-block-surecart-product-review-form"))):(document.body.classList.remove("sc-product-review-form-open"),i.forEach((e=>{e.removeAttribute("inert")})),i=[])},handleKeyDown(e){"Escape"===e?.key&&m.close(e)},clearForm(){const e=(0,a.getContext)();e.stars=0,e.title="",e.body="",document.querySelectorAll('.wp-block-surecart-product-review-form-rating input[type="radio"][name="stars"]').forEach((e=>{e.checked=!1}))}}});