import{r as i,c as s,h as t,g as e}from"./p-b891c2f1.js";import{_ as o}from"./p-684c85b4.js";import"./p-aafd8ea4.js";import{o as n}from"./p-494248b1.js";import"./p-112455b1.js";const l=class{constructor(t){i(this,t),this.scRemoveLineItem=s(this,"scRemoveLineItem",7),this.scUpdateLineItem=s(this,"scUpdateLineItem",7),this.scAddLineItem=s(this,"scAddLineItem",7),this.currencyCode="usd",this.lineItems=[],this.removeInvalid=!0}async reportValidity(){return!this.input||this.input.shadowRoot.querySelector("sc-input").reportValidity()}handleChange(){const i=Array.from(this.getChoices()).find((i=>i.checked));this.showCustomAmount="ad_hoc"===i.value,isNaN(parseInt(i.value))||this.scUpdateLineItem.emit({price_id:this.priceId,quantity:1,ad_hoc_amount:parseInt(i.value)})}handleCustomAmountToggle(i){i&&setTimeout((()=>{var i,s;null===(s=null===(i=this.input)||void 0===i?void 0:i.triggerFocus)||void 0===s||s.call(i)}),50)}handleLineItemsChange(){var i;(null===(i=this.lineItems)||void 0===i?void 0:i.length)&&(this.lineItem=(this.lineItems||[]).find((i=>i.price.id===this.priceId)))}handleLineItemChange(i){this.removeInvalid&&this.removeInvalidPrices();const s=this.getChoices();let t=!1;s.forEach((s=>{isNaN(parseInt(s.value))||s.disabled||(parseInt(s.value)===(null==i?void 0:i.ad_hoc_amount)?(s.checked=!0,t=!0):s.checked=!1)})),this.showCustomAmount=!t,t||(this.el.querySelector('sc-choice[value="ad_hoc"]').checked=!0)}componentWillLoad(){this.handleLineItemsChange()}selectDefaultChoice(){const i=this.getChoices();i.length&&(i[0].checked=!0)}getChoices(){return this.el.querySelectorAll("sc-choice")||[]}removeInvalidPrices(){this.lineItem&&this.getChoices().forEach((i=>{var s,t,e,o,n,l,a,h;return(null===(t=null===(s=this.lineItem)||void 0===s?void 0:s.price)||void 0===t?void 0:t.ad_hoc_max_amount)&&parseInt(i.value)>(null===(o=null===(e=this.lineItem)||void 0===e?void 0:e.price)||void 0===o?void 0:o.ad_hoc_max_amount)||(null===(l=null===(n=this.lineItem)||void 0===n?void 0:n.price)||void 0===l?void 0:l.ad_hoc_min_amount)&&parseInt(i.value)<(null===(h=null===(a=this.lineItem)||void 0===a?void 0:a.price)||void 0===h?void 0:h.ad_hoc_min_amount)?(i.style.display="none",void(i.disabled=!0)):(i.style.display="flex",void(i.disabled=!1))}))}updateCustomAmount(){var i;this.input.value!==(null===(i=this.lineItem)||void 0===i?void 0:i.ad_hoc_amount.toString())&&(this.input.value?this.scUpdateLineItem.emit({price_id:this.priceId,quantity:1,ad_hoc_amount:parseInt(this.input.value)}):this.scRemoveLineItem.emit({price_id:this.priceId,quantity:1}))}render(){var i;return this.loading?t("div",{class:"sc-donation-choices"},t("sc-skeleton",{style:{width:"20%",display:"inline-block"}}),t("sc-skeleton",{style:{width:"60%",display:"inline-block"}}),t("sc-skeleton",{style:{width:"40%",display:"inline-block"}})):t("div",{class:"sc-donation-choices"},t("sc-choices",{label:this.label,"auto-width":!0},t("slot",null)),this.showCustomAmount&&t("div",{class:"sc-donation-choices__form"},t("sc-price-input",{ref:i=>this.input=i,required:!0,currencyCode:this.currencyCode,label:"Enter an amount",value:null===(i=this.lineItem)||void 0===i?void 0:i.ad_hoc_amount.toString()}),t("sc-button",{type:"primary",onClick:()=>this.updateCustomAmount(),full:!0,busy:this.busy},o("Update","surecart"))),this.busy&&t("sc-block-ui",{style:{zIndex:"9"}}))}get el(){return e(this)}static get watchers(){return{showCustomAmount:["handleCustomAmountToggle"],lineItems:["handleLineItemsChange"],lineItem:["handleLineItemChange"]}}};n(l,["lineItems","loading","busy","currencyCode"],!1),l.style=":host{display:block}.sc-donation-choices{display:grid;gap:var(--sc-spacing-small);position:relative}.sc-donation-choices__form{display:grid;gap:var(--sc-spacing-small)}";export{l as sc_donation_choices}