(()=>{"use strict";var e={20:(e,t,n)=>{var o=n(609),i=Symbol.for("react.element"),c=Symbol.for("react.fragment"),r=Object.prototype.hasOwnProperty,s=o.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,a={key:!0,ref:!0,__self:!0,__source:!0};function l(e,t,n){var o,c={},l=null,u=null;for(o in void 0!==n&&(l=""+n),void 0!==t.key&&(l=""+t.key),void 0!==t.ref&&(u=t.ref),t)r.call(t,o)&&!a.hasOwnProperty(o)&&(c[o]=t[o]);if(e&&e.defaultProps)for(o in t=e.defaultProps)void 0===c[o]&&(c[o]=t[o]);return{$$typeof:i,type:e,key:l,ref:u,props:c,_owner:s.current}}t.Fragment=c,t.jsx=l,t.jsxs=l},338:(e,t,n)=>{var o=n(795);t.H=o.createRoot,o.hydrateRoot},609:e=>{e.exports=window.React},795:e=>{e.exports=window.ReactDOM},848:(e,t,n)=>{e.exports=n(20)}},t={};function n(o){var i=t[o];if(void 0!==i)return i.exports;var c=t[o]={exports:{}};return e[o](c,c.exports,n),c.exports}var o=n(338);const i=window.wp.plugins;var c=n(609);const r=window.wp.i18n,s=window.wp.components,a=window.wp.data,l=window.wp.url,u=window.wc.blocksCheckout;var d=n(848);const m=({lineItem:e})=>{const[t,n]=(0,c.useState)(!1),[o,i]=(0,c.useState)(!1),[m,p]=(0,c.useState)(""),[w,_]=(0,c.useState)(!0),g="invalid-gifting-recipient",{setValidationErrors:f,clearValidationError:b}=(0,a.useDispatch)("wc/store/validation"),h=e?.querySelector(".wc-block-components-product-details__item-key .wc-block-components-product-details__value")?.textContent||"",v=(0,a.useSelect)((e=>{if(!h)return null;const t=e("wc/store/cart");if(!t)return null;try{const e=t.getCartData();return e?.items?.find((e=>e.key===h))||null}catch(e){return null}}),[h]),k=(0,c.useMemo)((()=>{if(!v?.item_data)return null;const e=v.item_data.find((e=>"gifting_to_hidden"===e.name));return e?.value||null}),[v]);(0,c.useEffect)((()=>{null!==k&&(p(k),n(!0))}),[k]),(0,c.useEffect)((()=>{if(!e)return;const t=e.querySelector(".wc-block-components-product-details__gifting-to-hidden");if(!t)return;const o=t.querySelector(".wc-block-components-product-details__value");p(o?o.textContent:""),n(!0)}),[e]);const x=e=>{if(!e&&!(0,l.isEmail)(m))return _(!1),void f({[g]:{message:(0,r.__)("Please enter a valid email address","woocommerce-subscriptions"),hidden:!1}});i(!1),_(!0),b(g),(0,u.extensionCartUpdate)({namespace:"wcsg-cart",data:{recipient:!0===e?"":m,itemKey:h}}).then((()=>{i(!1)})).catch((()=>{_(!1),i(!0),f({[g]:{message:(0,r.__)("Please enter a valid email address","woocommerce-subscriptions"),hidden:!1}})}))};return h?(0,d.jsxs)("div",{className:"wcsg-gifting-to-container",children:[(!t||o)&&(0,d.jsx)(s.CheckboxControl,{"aria-label":window.wcSettings.wcsg_subscriptions_data.gifting_checkbox_text||(0,r.__)("This is a gift","woocommerce-subscriptions"),label:window.wcSettings.wcsg_subscriptions_data.gifting_checkbox_text||(0,r.__)("This is a gift","woocommerce-subscriptions"),checked:t,onChange:e=>{n(e),i(e)}}),t&&!o&&(0,d.jsxs)("div",{className:"wcsg-gifting-to-container-view",children:[(0,d.jsxs)("span",{title:(0,r.__)("Gifting to","woocommerce-subscriptions"),children:[(0,r.__)("Gifting to:","woocommerce-subscriptions")," ",!o&&m]}),(0,d.jsx)(s.Button,{variant:"link",onClick:()=>i(!0),children:(0,r.__)("Edit","woocommerce-subscriptions")}),(0,d.jsx)(s.Button,{variant:"link","aria-label":(0,r.__)("Remove gift recipient","woocommerce-subscriptions"),onClick:()=>(n(!1),p(""),void x(!0)),children:(0,r.__)("Remove","woocommerce-subscriptions")})]}),o&&(0,d.jsxs)("div",{className:"wcsg-gifting-to-container-editing",children:[(0,d.jsx)(s.TextControl,{type:"email",className:"wc-block-components-text-input "+(w?"":"has-error"),value:m,onChange:e=>p(e),onFocus:()=>_(!0),placeholder:(0,r.__)("Recipient's email address","woocommerce-subscriptions"),"aria-label":(0,r.__)("Gifting recipient","woocommerce-subscriptions"),role:"textbox"}),(0,d.jsx)("button",{className:"wc-block-components-button wp-element-button gifting-update-button",onClick:()=>x(!1),name:"Update",children:(0,r.__)("Update","woocommerce-subscriptions")})]}),o&&!w&&(0,d.jsx)(u.ValidationInputError,{elementId:g,propertyName:g})]}):null},p=()=>{const{__internalSetUseShippingAsBilling:e,setEditingShippingAddress:t}=(0,a.useDispatch)("wc/store/checkout"),n=(0,a.useSelect)((e=>{const t=e("wc/store/cart");if(!t)return null;try{const e=t.getCartData();return e?.items?.find((e=>e.item_data.find((e=>"gifting_to_hidden"===e.name))))||null}catch(e){return null}}),[]);return(0,c.useEffect)((()=>{n&&(e(!1),t(!0))}),[n,e,t]),(0,d.jsx)(d.Fragment,{})};window.addEventListener("load",(function(){let e=null;function t(){const t=document.querySelectorAll(".wc-block-components-product-metadata");0!==t.length&&(t.length&&e&&e.disconnect(),t.forEach((function(e,t){const n="gifting-recipient-"+t;if(!document.getElementById(n)){const t=document.createElement("div");t.className="wcsg-block-recipient-container",t.id=n,e.parentNode.insertBefore(t,e)}(0,o.H)(document.getElementById(n)).render((0,d.jsx)(m,{lineItem:e}))})))}function n(){setTimeout((()=>{e=new MutationObserver(t),t(),e.observe(document.body,{childList:!0,subtree:!0})}),200)}null!==document.querySelector(".wp-block-woocommerce-checkout")?(0,i.registerPlugin)("woocommerce-checkout-gifting",{render:p,icon:"gift",title:"Checkout Gifting Features",scope:"woocommerce-checkout"}):(n(),document.addEventListener("click",(function(e){e.target.matches(".wc-block-mini-cart__button, .wc-block-mini-cart__button *")&&n()})))}))})();