import{proxyCustomElement,HTMLElement,h,Host}from"@stencil/core/internal/client";import{o as onChange$1,s as state$1}from"./mutations2.js";import{l as lockCheckout,b as unLockCheckout}from"./mutations4.js";import{o as onChange,s as state}from"./store2.js";import{d as createOrUpdateCheckout}from"./index5.js";import{g as getCurrentCustomer,h as hasAddressData,i as isAddressEmpty}from"./index7.js";const ScCheckoutAutofillProvider=proxyCustomElement(class extends HTMLElement{constructor(){super(),this.__registerHost(),this.__attachShadow()}componentWillLoad(){this.maybeApplyProfile(),this.removeUserListener=onChange("loggedIn",(e=>{e?this.maybeApplyProfile():this.appliedForCheckoutId=void 0})),this.removeCheckoutListener=onChange$1("checkout",(()=>this.maybeApplyProfile()))}disconnectedCallback(){var e,t;null===(e=this.removeUserListener)||void 0===e||e.call(this),null===(t=this.removeCheckoutListener)||void 0===t||t.call(this)}async maybeApplyProfile(){var e,t,o,s,i,l,a,n;if(!state.loggedIn)return;const r=null===(e=state$1.checkout)||void 0===e?void 0:e.id;if(r&&this.appliedForCheckoutId!==r){this.appliedForCheckoutId=r;try{const e=await getCurrentCustomer(state$1.mode),r={},d=null==e?void 0:e.shipping_address,c=null==e?void 0:e.billing_address;if(hasAddressData(d)&&isAddressEmpty(null===(t=state$1.checkout)||void 0===t?void 0:t.shipping_address)&&(r.shipping_address=d),hasAddressData(c)&&isAddressEmpty(null===(o=state$1.checkout)||void 0===o?void 0:o.billing_address)&&(r.billing_address=c),(null==e?void 0:e.first_name)&&!(null===(s=state$1.checkout)||void 0===s?void 0:s.first_name)&&(r.first_name=e.first_name),(null==e?void 0:e.last_name)&&!(null===(i=state$1.checkout)||void 0===i?void 0:i.last_name)&&(r.last_name=e.last_name),(null==e?void 0:e.phone)&&!(null===(l=state$1.checkout)||void 0===l?void 0:l.phone)&&(r.phone=e.phone),"boolean"==typeof(null==e?void 0:e.billing_matches_shipping)&&(null===(a=state$1.checkout)||void 0===a?void 0:a.billing_matches_shipping)!==e.billing_matches_shipping&&(r.billing_matches_shipping=e.billing_matches_shipping),!Object.keys(r).length)return;lockCheckout("customer-profile-autofill");try{state$1.checkout=await createOrUpdateCheckout({id:null===(n=state$1.checkout)||void 0===n?void 0:n.id,data:r})}finally{unLockCheckout("customer-profile-autofill")}}catch(e){this.appliedForCheckoutId=void 0,console.error(e)}}}render(){return h(Host,{key:"9c330e8ab3d753d44009663266b94c7f07492230"},h("slot",{key:"408db421d26ecaa8df18c8d2ac7c1ac9be170a23"}))}},[1,"sc-checkout-autofill-provider"]);function defineCustomElement(){"undefined"!=typeof customElements&&["sc-checkout-autofill-provider"].forEach((e=>{"sc-checkout-autofill-provider"===e&&(customElements.get(e)||customElements.define(e,ScCheckoutAutofillProvider))}))}export{ScCheckoutAutofillProvider as S,defineCustomElement as d};