import{r as t,h as i,F as s}from"./p-b891c2f1.js";import{_ as e}from"./p-684c85b4.js";import"./p-aafd8ea4.js";import{a as n}from"./p-6fd3642a.js";import{a as o}from"./p-1c2e2695.js";import"./p-112455b1.js";const r=class{constructor(i){t(this,i)}componentWillLoad(){this.fetchItems()}async fetchItems(){try{this.loading=!0,await Promise.all([this.fetchProtocol(),this.fetchSubscription()])}catch(t){console.error(t),this.error=(null==t?void 0:t.message)||e("Something went wrong","surecart")}finally{this.loading=!1}}async fetchProtocol(){this.protocol=await n({path:"/surecart/v1/subscription_protocol"})}async fetchSubscription(){this.subscriptionId&&(this.subscription=await n({path:o(`/surecart/v1/subscriptions/${this.subscriptionId}`,{expand:["price","price.product","latest_invoice","product"]})}))}async cancelSubscription(){try{this.error="",this.busy=!0,await n({path:`/surecart/v1/subscriptions/${this.subscriptionId}/cancel`,method:"PATCH"}),this.successUrl?window.location.assign(this.successUrl):this.busy=!1}catch(t){this.error=(null==t?void 0:t.message)||e("Something went wrong","surecart"),this.busy=!1}}renderContent(){var t,n;return this.loading?this.renderLoading():i(s,null,i("sc-subscription-details",{subscription:null==this?void 0:this.subscription,hideRenewalText:!0}),"pending"===(null===(t=null==this?void 0:this.protocol)||void 0===t?void 0:t.cancel_behavior)?i("sc-alert",{type:"info",open:!0},e("Your plan will be canceled, but is still available until the end of your billing period on","surecart")," ",i("strong",null,i("sc-format-date",{type:"timestamp",date:null===(n=null==this?void 0:this.subscription)||void 0===n?void 0:n.current_period_end_at,month:"long",day:"numeric",year:"numeric"})),". ",e("If you change your mind, you can renew your subscription.","surecart")):i("sc-alert",{type:"info",open:!0},e("Your plan will be canceled immediately. You cannot change your mind.","surecart")))}renderLoading(){return i("div",{style:{padding:"0.5em"}},i("sc-skeleton",{style:{width:"30%",marginBottom:"0.75em"}}),i("sc-skeleton",{style:{width:"20%",marginBottom:"0.75em"}}),i("sc-skeleton",{style:{width:"40%"}}))}render(){return i("sc-dashboard-module",{heading:this.heading||e("Cancel your plan","surecart"),class:"subscription-cancel",error:this.error},i("sc-card",null,this.renderContent(),i("sc-button",{type:"primary",full:!0,loading:this.loading||this.busy,disabled:this.loading||this.busy,onClick:()=>this.cancelSubscription()},e("Cancel Plan","surecart")),!!this.backUrl&&i("sc-button",{href:this.backUrl,full:!0,loading:this.loading||this.busy,disabled:this.loading||this.busy},e("Go Back","surecart"))),this.busy&&i("sc-block-ui",null))}};r.style=":host{display:block;position:relative}.subscription-cancel{display:grid;gap:0.5em}";export{r as sc_subscription_cancel}