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,this.shippingChoices=void 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;const s=this.shippingChoices||(null===(t=null===(e=null==checkoutState?void 0:checkoutState.checkout)||void 0===e?void 0:e.shipping_choices)||void 0===t?void 0:t.data);if(!this.shippingChoices){if(!(null===(i=null==checkoutState?void 0:checkoutState.checkout)||void 0===i?void 0:i.selected_shipping_choice_required))return h(Host,{style:{display:"none"}});if(!(null==s?void 0:s.length)&&!(null===(c=null===(o=null==checkoutState?void 0:checkoutState.checkout)||void 0===o?void 0:o.shipping_address)||void 0===c?void 0:c.country))return 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")));if(!(null==s?void 0:s.length))return 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")))}return h(Host,null,h("sc-radio-group",{part:"base",label:this.label||__("Shipping","surecart"),class:"shipping-choices",onScChange:e=>this.updateCheckout(e.detail)},(s||[]).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))}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"},shippingChoices:{type:"unknown",mutable:!1,complexType:{original:"any[] | undefined",resolved:"any[]",references:{}},required:!1,optional:!1,docs:{tags:[],text:"Override shipping choices data (used for editor preview)"}}}}}