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:"a29f7c029f0d4d31e0954a562a8413f101ef127d",order:checkoutState.checkout,onScUpdateLineItems:e=>this.loadUpdate({line_items:e.detail})},h("slot",{key:"52312ff0130b58f3ede57e900dab062c7929200b"}))}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}]}}