import{Fragment,h}from"@stencil/core";import{__}from"@wordpress/i18n";import apiFetch from"../../../../functions/fetch";import{addQueryArgs}from"@wordpress/url";export class ScSubscriptionReactivate{constructor(){this.open=void 0,this.subscription=void 0,this.busy=void 0,this.error=void 0,this.upcomingPeriod=void 0,this.loading=!1}openChanged(){this.open&&this.fetchUpcoming()}async fetchUpcoming(){var e;this.loading=!0;try{this.upcomingPeriod=await apiFetch({method:"PATCH",path:addQueryArgs(`surecart/v1/subscriptions/${null===(e=this.subscription)||void 0===e?void 0:e.id}/upcoming_period`,{skip_product_group_validation:!0,expand:["period.checkout"]}),data:{purge_pending_update:!1}})}catch(e){this.error=(null==e?void 0:e.message)||__("Something went wrong","surecart")}finally{this.loading=!1}}async reactivateSubscription(){var e;try{this.error="",this.busy=!0,await apiFetch({path:`surecart/v1/subscriptions/${null===(e=this.subscription)||void 0===e?void 0:e.id}/restore`,method:"PATCH"}),this.scRefresh.emit(),this.scRequestClose.emit("close-button")}catch(e){this.error=(null==e?void 0:e.message)||__("Something went wrong","surecart")}finally{this.busy=!1}}renderLoading(){return h("sc-flex",{flexDirection:"column",style:{gap:"1em"}},h("sc-skeleton",{style:{width:"20%",display:"inline-block"}}),h("sc-skeleton",{style:{width:"60%",display:"inline-block"}}),h("sc-skeleton",{style:{width:"40%",display:"inline-block"}}))}render(){var e,t,i,s;return h("sc-dialog",{key:"4037e8e5a4f5ed97a2ab2ffaf57cba73c976c461",noHeader:!0,open:this.open,style:{"--width":"600px","--body-spacing":"var(--sc-spacing-xxx-large)"}},h("sc-dashboard-module",{key:"d2550a955aacaa20f8d744855017e05fc113564b",loading:this.loading,heading:__("Resubscribe","surecart"),class:"subscription-reactivate",error:this.error,style:{"--sc-dashboard-module-spacing":"1em"}},this.loading?this.renderLoading():h(Fragment,null,h("div",{slot:"description"},h("sc-alert",{open:!0,type:"warning",title:__("Confirm Charge","surecart")},__("You will be charged","surecart")," ",h("sc-format-number",{type:"currency",value:null===(t=null===(e=this.upcomingPeriod)||void 0===e?void 0:e.checkout)||void 0===t?void 0:t.amount_due,currency:null===(i=this.subscription)||void 0===i?void 0:i.currency})," ",__("immediately for your subscription.","surecart")),h("sc-text",{style:{"--font-size":"var(--sc-font-size-medium)","--color":"var(--sc-input-label-color)","--line-height":"var(--sc-line-height-dense)","margin-top":"var(--sc-spacing-medium)"}},__("Your subscription will be reactivated and will renew automatically on","surecart")," ",h("strong",null,null===(s=this.upcomingPeriod)||void 0===s?void 0:s.end_at_date))),h("sc-flex",{justifyContent:"flex-start"},h("sc-button",{type:"primary",loading:this.busy,disabled:this.busy,onClick:()=>this.reactivateSubscription()},__("Yes, Reactivate","surecart")),h("sc-button",{disabled:this.busy,style:{color:"var(--sc-color-gray-500)"},type:"text",onClick:()=>this.scRequestClose.emit()},__("No, Keep Inactive","surecart")))),this.busy&&h("sc-block-ui",{key:"4e1cdd7eddb7670915ceea2f8ab2f64ddbde1128"})))}static get is(){return"sc-subscription-reactivate"}static get encapsulation(){return"shadow"}static get properties(){return{open:{type:"boolean",mutable:!1,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:"Whether it is open"},attribute:"open",reflect:!1},subscription:{type:"unknown",mutable:!1,complexType:{original:"Subscription",resolved:"Subscription",references:{Subscription:{location:"import",path:"src/types",id:"src/types.ts::Subscription"}}},required:!1,optional:!1,docs:{tags:[],text:"The subscription to reactivate"}}}}static get states(){return{busy:{},error:{},upcomingPeriod:{},loading:{}}}static get events(){return[{method:"scRequestClose",name:"scRequestClose",bubbles:!0,cancelable:!0,composed:!0,docs:{tags:[],text:"Reactivate modal closed"},complexType:{original:"'close-button' | 'keyboard' | 'overlay'",resolved:'"close-button" | "keyboard" | "overlay"',references:{}}},{method:"scRefresh",name:"scRefresh",bubbles:!0,cancelable:!0,composed:!0,docs:{tags:[],text:"Refresh subscriptions"},complexType:{original:"void",resolved:"void",references:{}}}]}static get watchers(){return[{propName:"open",methodName:"openChanged"}]}}