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:c}=checkoutState.checkout;
/** translators: %1$s: formatted amount */speak(sprintf(__("Your order total has changed to: %1$s.","surecart"),getFormattedPrice({amount:t,currency:c})),"assertive")}catch(e){console.error(e),createErrorNotice(e)}finally{unLockCheckout("selected_shipping_choice")}}render(){var e,t,c,o,i,s,r,a,n,l,p;return(null===(e=null==checkoutState?void 0:checkoutState.checkout)||void 0===e?void 0:e.selected_shipping_choice_required)?(null===(o=null===(c=null===(t=null==checkoutState?void 0:checkoutState.checkout)||void 0===t?void 0:t.shipping_choices)||void 0===c?void 0:c.data)||void 0===o?void 0:o.length)||(null===(s=null===(i=null==checkoutState?void 0:checkoutState.checkout)||void 0===i?void 0:i.shipping_address)||void 0===s?void 0:s.country)?(null===(n=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===n?void 0:n.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===(l=null==checkoutState?void 0:checkoutState.checkout)||void 0===l?void 0:l.shipping_choices)||void 0===p?void 0:p.data)||[]).map((({id:e,amount:t,currency:c,shipping_method:o})=>{var i;return h("sc-radio",{key:e,checked:(null===(i=null==checkoutState?void 0:checkoutState.checkout)||void 0===i?void 0:i.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==o?void 0:o.name)||__("Standard Shipping","surecart")),this.showDescription&&!!(null==o?void 0:o.description)&&h("div",{class:"shipping-choice__description"},null==o?void 0:o.description)),h("div",{class:"shipping-choice__price"},t?h("sc-format-number",{type:"currency",value:t,currency:c}):__("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"}}}}