import{h}from"@stencil/core";import{__}from"@wordpress/i18n";import{state as checkoutState}from"../../../store/checkout/index";import{updateCheckout}from"../../../services/session";import{createErrorNotice}from"../../../store/notices/mutations";import{updateFormState}from"../../../store/form/mutations";import{clearCheckout}from"../../../store/checkout/mutations";export class ScCartSessionProvider{handleUpdateSession(e){const{data:t,options:o}=e.detail;(null==o?void 0:o.silent)?this.update(t):this.loadUpdate(t)}handleErrorResponse(e){var t,o;"readonly"!==(null==e?void 0:e.code)&&"checkout.customer.account_mismatch"!==(null===(o=null===(t=null==e?void 0:e.additional_errors)||void 0===t?void 0:t[0])||void 0===o?void 0:o.code)||clearCheckout(),"rest_cookie_invalid_nonce"!==(null==e?void 0:e.code)?((null==e?void 0:e.message)&&createErrorNotice(e),"http_request_failed"===(null==e?void 0:e.code)&&createErrorNotice(__("Something went wrong. Please reload the page and try again.","surecart"))):updateFormState("EXPIRE")}async fetch(e={}){this.loadUpdate({status:"draft",...e})}async update(e={},t={}){var o;try{checkoutState.checkout=await updateCheckout({id:null===(o=checkoutState.checkout)||void 0===o?void 0:o.id,data:{...e},query:{...t}})}catch(e){throw console.error(e),e}}async loadUpdate(e={}){try{updateFormState("FETCH"),await this.update(e),updateFormState("RESOLVE")}catch(e){updateFormState("REJECT"),this.handleErrorResponse(e)}}render(){return h("sc-line-items-provider",{key:"4dda79c598a800ae12f388ccfafb4f9cd1dc854a",order:checkoutState.checkout,onScUpdateLineItems:e=>this.loadUpdate({line_items:e.detail})},h("slot",{key:"b7e4a92273cf7dc6e0d80a9f0d6e2dc6eb8aec86"}))}static get is(){return"sc-cart-session-provider"}static get encapsulation(){return"shadow"}static get events(){return[{method:"scSetState",name:"scSetState",bubbles:!0,cancelable:!0,composed:!0,docs:{tags:[],text:"Set the state"},complexType:{original:"'loading' | 'busy' | 'navigating' | 'idle'",resolved:'"busy" | "idle" | "loading" | "navigating"',references:{}}}]}static get elementRef(){return"el"}static get listeners(){return[{name:"scUpdateOrder",method:"handleUpdateSession",target:void 0,capture:!1,passive:!1}]}}