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