import{h,Host}from"@stencil/core";import{getProductBuyLink,submitCartForm}from"../../../../store/product/mutations";import{state}from"../../../../store/product/index";import{setProduct}from"../../../../store/product/setters";import{onChange}from"../../../../store/product/index";import{isProductOutOfStock,isSelectedVariantMissing}from"../../../../store/product/getters";import{getTopLevelError,getAdditionalErrorMessages}from"../../../../functions/error";export class ScProductBuyButton{constructor(){this.addToCart=void 0,this.productId=void 0,this.formId=void 0,this.mode="live",this.checkoutLink=void 0,this.error=void 0}async handleCartClick(t){var e,r,o,i,d;if(t.preventDefault(),console.log(t),!(null===(e=state[this.productId])||void 0===e?void 0:e.busy))if(null===(o=null===(r=state[this.productId])||void 0===r?void 0:r.selectedPrice)||void 0===o?void 0:o.ad_hoc)setProduct(this.productId,{dialog:this.addToCart?"ad_hoc_cart":"ad_hoc_buy"});else{if(!this.addToCart){const t=null===(d=null===(i=null===window||void 0===window?void 0:window.scData)||void 0===i?void 0:i.pages)||void 0===d?void 0:d.checkout;if(!t)return;return window.location.assign(getProductBuyLink(this.productId,t,{no_cart:!this.addToCart}))}try{await submitCartForm(this.productId)}catch(t){console.error(t),this.error=t}}}componentDidLoad(){this.link=this.el.querySelector("a"),this.updateProductLink(),onChange(this.productId,(()=>this.updateProductLink()))}updateProductLink(){var t,e;const r=null===(e=null===(t=null===window||void 0===window?void 0:window.scData)||void 0===t?void 0:t.pages)||void 0===e?void 0:e.checkout;r&&this.link&&(this.link.href=getProductBuyLink(this.productId,r,this.addToCart?{}:{no_cart:!0}))}render(){var t,e;return h(Host,{key:"8aea1a81f802ca0eb4415359db962396e58f7db2",class:{"is-busy":(null===(t=state[this.productId])||void 0===t?void 0:t.busy)&&!!this.addToCart,"is-disabled":null===(e=state[this.productId])||void 0===e?void 0:e.disabled,"is-sold-out":isProductOutOfStock(this.productId)&&!isSelectedVariantMissing(this.productId),"is-unavailable":isSelectedVariantMissing(this.productId)},onClick:t=>this.handleCartClick(t)},!!this.error&&h("sc-alert",{key:"4209dd9bc56d54c33c9c7bb052101ad25d57a7fb",onClick:t=>{t.stopPropagation()},type:"danger",scrollOnOpen:!0,open:!!this.error,closable:!1},!!getTopLevelError(this.error)&&h("span",{key:"476d72435c03b9b30c0c3c66edfcaa1fdaa2aa24",slot:"title",innerHTML:getTopLevelError(this.error)}),(getAdditionalErrorMessages(this.error)||[]).map(((t,e)=>h("div",{innerHTML:t,key:e})))),h("slot",{key:"0db31ac0ca82fbba425cfe5273e1a8565c97631a"}))}static get is(){return"sc-product-buy-button"}static get originalStyleUrls(){return{$:["sc-product-buy-button.scss"]}}static get styleUrls(){return{$:["sc-product-buy-button.css"]}}static get properties(){return{addToCart:{type:"boolean",mutable:!1,complexType:{original:"boolean",resolved:"boolean",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"add-to-cart",reflect:!1},productId:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"product-id",reflect:!1},formId:{type:"number",mutable:!1,complexType:{original:"number",resolved:"number",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"form-id",reflect:!1},mode:{type:"string",mutable:!1,complexType:{original:"'live' | 'test'",resolved:'"live" | "test"',references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"mode",reflect:!1,defaultValue:"'live'"},checkoutLink:{type:"string",mutable:!1,complexType:{original:"string",resolved:"string",references:{}},required:!1,optional:!1,docs:{tags:[],text:""},attribute:"checkout-link",reflect:!1}}}static get states(){return{error:{}}}static get elementRef(){return"el"}}