import{h}from"@stencil/core";import{__,_n,sprintf}from"@wordpress/i18n";import{addQueryArgs}from"@wordpress/url";import apiFetch from"../../../../functions/fetch";import{intervalString}from"../../../../functions/price";import{getBundleComponentRowsFromBundleItems}from"../../../../functions/line-items";import{productNameWithPrice}from"../../../../functions/price";import{formatNumber}from"../../../../../../admin/util";export class ScSubscriptionDetails{constructor(){this.subscription=void 0,this.pendingPrice=void 0,this.hideRenewalText=void 0,this.activationsModal=void 0,this.loading=void 0,this.hasPendingUpdate=void 0,this.bundleExpanded=!1}renderName(){var i,e,t;return"string"!=typeof(null===(e=null===(i=this.subscription)||void 0===i?void 0:i.price)||void 0===e?void 0:e.product)?productNameWithPrice(null===(t=this.subscription)||void 0===t?void 0:t.price):__("Subscription","surecart")}async handleSubscriptionChange(){var i,e,t,n;this.hasPendingUpdate=!!(null===(e=Object.keys((null===(i=null==this?void 0:this.subscription)||void 0===i?void 0:i.pending_update)||{}))||void 0===e?void 0:e.length),!(null===(n=null===(t=null==this?void 0:this.subscription)||void 0===t?void 0:t.pending_update)||void 0===n?void 0:n.price)||(null==this?void 0:this.pendingPrice)||this.hideRenewalText||(this.pendingPrice=await this.fetchPrice(this.subscription.pending_update.price))}componentWillLoad(){this.handleSubscriptionChange()}async fetchPrice(i){try{return this.loading=!0,await apiFetch({path:addQueryArgs(`surecart/v1/prices/${i}`,{expand:["product"]})})}catch(i){console.error(i)}finally{this.loading=!1}}renderRenewalText(){var i,e,t,n,s,o,r,d,a,l,c,u,p,v,b,_,f,g,m,y,w,_x,x;const P=h("sc-subscription-status-badge",{subscription:null==this?void 0:this.subscription});return(null===(i=null==this?void 0:this.subscription)||void 0===i?void 0:i.cancel_at_period_end)&&(null===(e=null==this?void 0:this.subscription)||void 0===e?void 0:e.current_period_end_at)?h("span",{"aria-label":sprintf(
/* translators: %s: current period end date */
__("Renewal Update - Your plan will be canceled on %s","surecart"),this.subscription.current_period_end_at_date)},P," ",
/* translators: %s: current period end date */
sprintf(__("Your plan will be canceled on %s","surecart"),this.subscription.current_period_end_at_date)):this.hasPendingUpdate?this.pendingPrice||(null===(n=null===(t=this.subscription)||void 0===t?void 0:t.pending_update)||void 0===n?void 0:n.ad_hoc_amount)?(null===(o=null===(s=this.subscription)||void 0===s?void 0:s.pending_update)||void 0===o?void 0:o.ad_hoc_amount)?h("span",{"aria-label":sprintf(
/* translators: 1: new price, 2: current period end date */
__("Renewal Update - Your plan switches to %1s on %2s","surecart"),formatNumber(null===(d=null===(r=this.subscription)||void 0===r?void 0:r.pending_update)||void 0===d?void 0:d.ad_hoc_amount,(null===(a=this.pendingPrice)||void 0===a?void 0:a.currency)||(null===(c=null===(l=this.subscription)||void 0===l?void 0:l.price)||void 0===c?void 0:c.currency)),this.subscription.current_period_end_at_date)},__("Your plan switches to","surecart")," ",h("strong",null,h("sc-format-number",{type:"currency",currency:(null===(u=this.pendingPrice)||void 0===u?void 0:u.currency)||(null===(v=null===(p=this.subscription)||void 0===p?void 0:p.price)||void 0===v?void 0:v.currency),value:null===(_=null===(b=this.subscription)||void 0===b?void 0:b.pending_update)||void 0===_?void 0:_.ad_hoc_amount})," ",intervalString(this.pendingPrice||(null===(f=this.subscription)||void 0===f?void 0:f.price)))," ",__("on","surecart")," ",this.subscription.current_period_end_at_date):h("span",{"aria-label":sprintf(
/* translators: 1: new plan name, 2: current period end date */
__("Renewal Update - Your plan switches to %1s on %2s","surecart"),this.pendingPrice.product.name,this.subscription.current_period_end_at_date)},__("Your plan switches to","surecart")," ",h("strong",null,this.pendingPrice.product.name)," ",__("on","surecart")," ",this.subscription.current_period_end_at_date):h("sc-skeleton",null):"trialing"===(null===(g=null==this?void 0:this.subscription)||void 0===g?void 0:g.status)&&(null===(m=null==this?void 0:this.subscription)||void 0===m?void 0:m.trial_end_at)?h("span",{"aria-label":sprintf(
/* translators: %s: trial end date */
__("Renewal Update - Your plan begins on %s.","surecart"),this.subscription.trial_end_at_date)},P," ",sprintf(
/* translators: %s: trial end date */
__("Your plan begins on %s","surecart"),null===(y=null==this?void 0:this.subscription)||void 0===y?void 0:y.trial_end_at_date)):"active"===(null===(w=this.subscription)||void 0===w?void 0:w.status)&&(null===(_x=this.subscription)||void 0===_x?void 0:_x.current_period_end_at)?h("span",{"aria-label":sprintf(
/* translators: %s: current period end date */
__("Renewal Update - Your next payment is on %s","surecart"),this.subscription.current_period_end_at_date)},P," ",null===(null===(x=this.subscription)||void 0===x?void 0:x.remaining_period_count)?/* translators: %s: current period end date */
sprintf(__("Your plan renews on %s","surecart"),this.subscription.current_period_end_at_date):/* translators: %s: current period end date */
sprintf(__("Your next payment is on %s","surecart"),this.subscription.current_period_end_at_date)):P}getActivations(){var i,e,t,n;return((null===(n=null===(t=null===(e=null===(i=this.subscription)||void 0===i?void 0:i.purchase)||void 0===e?void 0:e.license)||void 0===t?void 0:t.activations)||void 0===n?void 0:n.data)||[]).filter((i=>null==i?void 0:i.counted))}renderActivations(){var i;const e=this.getActivations();return(null==e?void 0:e.length)?h("sc-flex",{justifyContent:"flex-start",alignItems:"center"},h("sc-tag",{size:"small"},null===(i=null==e?void 0:e[0])||void 0===i?void 0:i.name),(null==e?void 0:e.length)>1&&h("sc-text",{style:{"--font-size":"var(--sc-font-size-small)",cursor:"pointer"},onClick:i=>{i.preventDefault(),i.stopImmediatePropagation(),this.activationsModal=!0}},"+ ",(null==e?void 0:e.length)-1," More")):null}getBundleItems(){var i,e,t;const n=null===(e=null===(i=this.subscription)||void 0===i?void 0:i.price)||void 0===e?void 0:e.product;return(null==n?void 0:n.bundle)&&(null===(t=null==n?void 0:n.bundle_items)||void 0===t?void 0:t.data)||[]}renderBundleComponents(){const i=getBundleComponentRowsFromBundleItems(this.getBundleItems());return i.length?h("div",null,h("button",{type:"button",class:"subscription-details__bundle-toggle","aria-expanded":this.bundleExpanded?"true":"false",onClick:i=>{i.preventDefault(),i.stopPropagation(),this.bundleExpanded=!this.bundleExpanded}},h("sc-icon",{name:this.bundleExpanded?"chevron-up":"chevron-down"}),sprintf(
/* translators: %d: number of bundle component items */
_n("Includes %d item","Includes %d items",i.length,"surecart"),i.length)),this.bundleExpanded&&h("div",{class:"subscription-details__bundle-components"},i.map((i=>h("div",{class:"bundle-component subscription-details__bundle-component",key:i.id},h("span",{class:"bundle-component__label"},i.name),i.qty>1&&h("span",{class:"bundle-component__qty subscription-details__bundle-qty"},sprintf(
/* translators: %d: quantity */
__("× %d","surecart"),i.qty))))))):null}showWarning(){var i,e,t,n,s,o,r,d;return!(null===(i=this.subscription)||void 0===i?void 0:i.payment_method)&&!(null===(e=this.subscription)||void 0===e?void 0:e.manual_payment)&&!!["active","past_due","unpaid","incomplete"].includes(null===(t=this.subscription)||void 0===t?void 0:t.status)&&((null===(s=null===(n=this.subscription)||void 0===n?void 0:n.price)||void 0===s?void 0:s.ad_hoc)?0!==(null===(o=this.subscription)||void 0===o?void 0:o.ad_hoc_amount):0!==(null===(d=null===(r=this.subscription)||void 0===r?void 0:r.price)||void 0===d?void 0:d.amount))}render(){return h("div",{key:"0af8aaeeffdd06301794468798575a49c655e791",class:"subscription-details"},this.hasPendingUpdate&&h("div",{key:"b543b7dfb1960beb1bf2ea7dba29384586d941e1"},h("sc-tag",{key:"32077623289e99dbf40619b88c2ff71a13c76fbb",size:"small",type:"warning"},__("Update Scheduled","surecart"))),h("sc-flex",{key:"2323ea492755eef1c6c6b00aa31e48d63403fc02",alignItems:"center",justifyContent:"flex-start"},h("sc-text",{key:"e974fb27e46435cdb057a1c722749516db98748f","aria-label":sprintf(
/* translators: %s: plan name */
__("Plan name - %s","surecart"),this.renderName()),style:{"--font-weight":"var(--sc-font-weight-bold)"}},this.renderName()),this.renderActivations()),!this.hideRenewalText&&h("div",{key:"a6f540c039f5435561f0d1375e9aeb79b89ca72e"},this.renderRenewalText()," "),this.renderBundleComponents(),h("slot",{key:"f3fd579417b5d1da39262eb9cb6ab8f038965d96"}),h("sc-dialog",{key:"3cf68ce55917febef8f773c786cfa604a0703d5c",label:__("Activations","surecart"),onScRequestClose:()=>this.activationsModal=!1,open:!!this.activationsModal},h("sc-card",{key:"4ce37429a8a8cfd4cd52bb47b2bc5667fdaa9f57","no-padding":!0,style:{"--overflow":"hidden"}},h("sc-stacked-list",{key:"ff19f56ad27ec67865f030beb132f715a3742a99"},(this.getActivations()||[]).map((i=>h("sc-stacked-list-row",{style:{"--columns":"2"},mobileSize:0},h("sc-text",{style:{"--line-height":"var(--sc-line-height-dense)"}},h("strong",null,null==i?void 0:i.name),h("div",null,h("sc-text",{style:{"--color":"var(--sc-color-gray-500)"}},null==i?void 0:i.fingerprint))),h("sc-text",{style:{"--color":"var(--sc-color-gray-500)"}},null==i?void 0:i.created_at_date))))))),this.showWarning()&&h("div",{key:"614dc9d79cd3acd0f99f50b8418b66b8086e8563"},h("sc-tag",{key:"79541f44622d0fc6dbe89e499a238da6d0479901",type:"warning"},h("div",{key:"c4cd2e30a88fc289ef894d0cefc6412e9c2ee97b",class:"subscription-details__missing-method"},h("sc-icon",{key:"f04ee1ef6bfa4821c302d2a97b87ff03f07a3d4e",name:"alert-triangle"}),__("Payment Method Missing","surecart")))))}static get is(){return"sc-subscription-details"}static get encapsulation(){return"shadow"}static get originalStyleUrls(){return{$:["sc-subscription-details.css"]}}static get styleUrls(){return{$:["sc-subscription-details.css"]}}static get properties(){return{subscription:{type:"unknown",mutable:!1,complexType:{original:"Subscription",resolved:"Subscription",references:{Subscription:{location:"import",path:"../../../../types",id:"src/types.ts::Subscription"}}},required:!1,optional:!1,docs:{tags:[],text:""}},pendingPrice:{type:"unknown",mutable:!1,complexType:{original:"Price",resolved:"Price",references:{Price:{location:"import",path:"../../../../types",id:"src/types.ts::Price"}}},required:!1,optional:!1,docs:{tags:[],text:""}},hideRenewalText:{type:"boolean",mutable:!1,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"hide-renewal-text",reflect:!1}}}static get states(){return{activationsModal:{},loading:{},hasPendingUpdate:{},bundleExpanded:{}}}static get watchers(){return[{propName:"subscription",methodName:"handleSubscriptionChange"}]}}