import{h,Host}from"@stencil/core";import{__,sprintf}from"@wordpress/i18n";import{state as checkoutState}from"../../../store/checkout/index";import{lockCheckout,unLockCheckout}from"../../../store/checkout/mutations";import{createOrUpdateCheckout}from"../../../services/session/index";import{checkoutIsLocked}from"../../../store/checkout/getters";import{createErrorNotice}from"../../../store/notices/mutations";import{speak}from"@wordpress/a11y";import{getFormattedPrice}from"../../../functions/price";export class ScShippingChoices{constructor(){this.label=void 0,this.showDescription=!0}async updateCheckout(e){if(e)try{lockCheckout("selected_shipping_choice"),checkoutState.checkout=await createOrUpdateCheckout({id:checkoutState.checkout.id,data:{selected_shipping_choice_id:e}}),speak(__("Shipping choice updated.","surecart"),"assertive");const{total_amount:t,currency:i}=checkoutState.checkout;
/** translators: %1$s: formatted amount */speak(sprintf(__("Your order total has changed to: %1$s.","surecart"),getFormattedPrice({amount:t,currency:i})),"assertive")}catch(e){console.error(e),createErrorNotice(e)}finally{unLockCheckout("selected_shipping_choice")}}render(){var e,t,i,o,c,s,r,a,l,n,p;return(null===(e=null==checkoutState?void 0:checkoutState.checkout)||void 0===e?void 0:e.selected_shipping_choice_required)?(null===(o=null===(i=null===(t=null==checkoutState?void 0:checkoutState.checkout)||void 0===t?void 0:t.shipping_choices)||void 0===i?void 0:i.data)||void 0===o?void 0:o.length)||(null===(s=null===(c=null==checkoutState?void 0:checkoutState.checkout)||void 0===c?void 0:c.shipping_address)||void 0===s?void 0:s.country)?(null===(l=null===(a=null===(r=null==checkoutState?void 0:checkoutState.checkout)||void 0===r?void 0:r.shipping_choices)||void 0===a?void 0:a.data)||void 0===l?void 0:l.length)?h(Host,null,h("sc-radio-group",{part:"base",label:this.label||__("Shipping","surecart"),class:"shipping-choices",onScChange:e=>this.updateCheckout(e.detail)},((null===(p=null===(n=null==checkoutState?void 0:checkoutState.checkout)||void 0===n?void 0:n.shipping_choices)||void 0===p?void 0:p.data)||[]).map((({id:e,display_amount:t,shipping_method:i})=>{var o;return h("sc-radio",{key:e,checked:(null===(o=null==checkoutState?void 0:checkoutState.checkout)||void 0===o?void 0:o.selected_shipping_choice)===e,exportparts:"base:radio__base,label:radio__label,control:radio__control,checked-icon:radio__checked-icon",class:"shipping-choice",value:e},h("div",{class:"shipping-choice__text"},h("div",{class:"shipping-choice__name"},(null==i?void 0:i.name)||__("Standard Shipping","surecart")),this.showDescription&&!!(null==i?void 0:i.description)&&h("div",{class:"shipping-choice__description"},null==i?void 0:i.description)),h("div",{class:"shipping-choice__price"},t||__("Free","surecart")))}))),checkoutIsLocked("selected_shipping_choice")&&h("sc-block-ui",null)):h("sc-form-control",{part:"empty",label:this.label||__("Shipping","surecart")},h("div",{class:"shipping-choice__empty"},__("Sorry, we are not able to ship to your address.","surecart"))):h("sc-form-control",{label:this.label||__("Shipping","surecart")},h("div",{class:"shipping-choice__empty"},__("To check available shipping choices, please provide your shipping country in the address section.","surecart"))):h(Host,{style:{display:"none"}})}static get is(){return"sc-shipping-choices"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-shipping-choices.scss"]}}static get styleUrls(){return{$:["sc-shipping-choices.css"]}}static get properties(){return{label:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:"The shipping section label"},attribute:"label",reflect:!1},showDescription:{type:"boolean",mutable:!1,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:"Whether to show the shipping choice description"},attribute:"show-description",reflect:!1,defaultValue:"true"}}}}