(()=>{"use strict";var e={n:o=>{var t=o&&o.__esModule?()=>o.default:()=>o;return e.d(t,{a:t}),t},d:(o,t)=>{for(var r in t)e.o(t,r)&&!e.o(o,r)&&Object.defineProperty(o,r,{enumerable:!0,get:t[r]})},o:(e,o)=>Object.prototype.hasOwnProperty.call(e,o)};const o=window.wp.blocks,t=window.wp.i18n,r=window.wp.blockEditor,n=window.wp.components,c=window.wp.element,s=window.wp.apiFetch;var i=e.n(s);const a=window.wp.hooks,l=window.wp.data,u=window.ReactJSXRuntime,d=JSON.parse('{"$schema":"https://schemas.wp.org/trunk/block.json","apiVersion":3,"name":"woocommerce/coupon-code","version":"1.0.0","title":"Coupon Code","category":"woocommerce","description":"Include a coupon code to entice customers to make a purchase.","supports":{"email":true,"html":false,"align":true,"color":{"text":true,"background":true},"typography":{"fontSize":true},"spacing":{"margin":true,"padding":true},"__experimentalBorder":{"color":true,"radius":true,"style":true,"width":true}},"attributes":{"couponCode":{"type":"string","default":""}},"textdomain":"woocommerce"}');(0,o.registerBlockType)(d.name,{...d,edit:function(e){const{attributes:o,setAttributes:s}=e,d=o.couponCode,{className:p="",style:m,...h}=(0,r.useBlockProps)(),[w,g]=(0,c.useState)(""),[b,x]=(0,c.useState)([]),[_,f]=(0,c.useState)(!1),y=(0,c.useRef)(null),v=(0,c.useRef)(null),k=(0,c.useCallback)((e=>{if(v.current&&v.current.abort(),e.length<2)return x([]),void f(!1);f(!0),v.current=new AbortController,i()({path:`/wc/v3/coupons?per_page=20&search=${encodeURIComponent(e)}`,signal:v.current.signal}).then((e=>{x(e),f(!1)})).catch((e=>{e instanceof Error&&"AbortError"===e.name||("rest_forbidden"!==e.code&&403!==e.status||(0,l.dispatch)("core/notices").createErrorNotice((0,t.__)("You do not have permission to view coupons.","woocommerce"),{id:"coupon-code-permission-error",type:"snackbar"}),f(!1))}))}),[]);(0,c.useEffect)((()=>(y.current&&clearTimeout(y.current),y.current=setTimeout((()=>{k(w)}),300),()=>{y.current&&clearTimeout(y.current)})),[w,k]),(0,c.useEffect)((()=>()=>{v.current&&v.current.abort()}),[]);const C=b.map((e=>({value:e.code,label:e.code})));d&&!C.some((e=>e.value===d))&&C.unshift({value:d,label:d});const{background:j,backgroundColor:S,border:B,...E}=m||{},N={fontSize:"1.2em",padding:"12px 20px",borderWidth:"2px",borderStyle:"dashed",borderColor:"#cccccc",borderRadius:"4px",color:"#000000",backgroundColor:"#f5f5f5",fontWeight:"bold",letterSpacing:"1px",...E,display:"inline-block",boxSizing:"border-box",textAlign:"center"},R=o.align,T={textAlign:["left","center","right","justify","start","end"].includes(R)?R:"center"},A=p.split(" ").filter(Boolean),F=[],O=[];A.forEach((e=>{e.startsWith("has-")||e.startsWith("wp-elements-")?F.push(e):O.push(e)}));const P=O.length>0?O.join(" "):void 0,z=F.length>0?F.join(" "):void 0;return(0,u.jsxs)(u.Fragment,{children:[(0,u.jsx)(r.InspectorControls,{children:(0,u.jsxs)(n.PanelBody,{title:(0,t.__)("Settings","woocommerce"),initialOpen:!0,children:[(0,u.jsxs)("div",{style:{marginBottom:"16px"},children:[(0,u.jsx)("div",{children:(0,t.__)("Search for an existing coupon","woocommerce")}),(0,u.jsx)(n.ComboboxControl,{label:(0,t.__)("Search coupons","woocommerce"),hideLabelFromVision:!0,value:d,onChange:e=>{s({couponCode:e||""})},onFilterValueChange:e=>{g(e)},options:C,__nextHasNoMarginBottom:!0,__next40pxDefaultSize:!0,help:_?(0,t.__)("Searching coupons…","woocommerce"):w.length>0&&w.length<2?(0,t.__)("Type at least 2 characters to search","woocommerce"):null}),_&&(0,u.jsx)("div",{style:{display:"flex",alignItems:"center",marginTop:"8px"},children:(0,u.jsx)(n.Spinner,{})})]}),(0,u.jsx)("div",{children:(0,u.jsx)(n.Button,{variant:"link",onClick:()=>{const e=(0,a.applyFilters)("woocommerce_email_editor_create_coupon_handler",(()=>{window.open("/wp-admin/post-new.php?post_type=shop_coupon","_blank")}));"function"==typeof e&&e()},style:{padding:0,height:"auto"},children:(0,t.__)("Create new coupon","woocommerce")})})]})}),(0,u.jsx)("div",{...h,className:P,style:{...h.style,...T},children:(0,u.jsx)("span",{className:z,style:N,children:d||(0,t.__)("Coupon Code block – No coupon selected","woocommerce")})})]})},save:function(e){const{attributes:o}=e,t=o.couponCode,n=r.useBlockProps.save();return(0,u.jsx)("div",{...n,children:t&&(0,u.jsx)("strong",{children:t})})}}),((this.wc=this.wc||{}).blocks=this.wc.blocks||{})["coupon-code"]={}})();