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;return h("sc-dialog",{key:"90c7bda8e1fab2340e273607fcc8b357371a8223",noHeader:!0,open:this.open,style:{"--width":"600px","--body-spacing":"var(--sc-spacing-xxx-large)"}},h("sc-dashboard-module",{key:"4d94f9622a06df54d50e8425d490eff039e8d5aa",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")," ",null===(t=null===(e=this.upcomingPeriod)||void 0===e?void 0:e.checkout)||void 0===t?void 0:t.amount_due_display_amount," ",__("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===(i=this.upcomingPeriod)||void 0===i?void 0:i.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:"aaece49196b03fdbcc195dd2995269d151c5dc3a"})))}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"}]}}