import{b as setProduct,s as state}from"./watchers.js";import{t as toggleCart}from"./mutations2.js";import{a as addCheckoutLineItem}from"./mutations4.js";import{a as addQueryArgs}from"./add-query-args.js";const submitCartForm=async e=>{var t,o,d,a,i,n;const r=state[e];if(r&&(null===(t=r.selectedPrice)||void 0===t?void 0:t.id)&&(!(null===(o=r.selectedPrice)||void 0===o?void 0:o.ad_hoc)||null!==r.adHocAmount&&void 0!==r.adHocAmount))try{setProduct(e,{busy:!0}),await addCheckoutLineItem({price:null===(d=r.selectedPrice)||void 0===d?void 0:d.id,quantity:Math.max((null===(a=r.selectedPrice)||void 0===a?void 0:a.ad_hoc)?1:r.quantity,1),...(null===(i=r.selectedPrice)||void 0===i?void 0:i.ad_hoc)?{ad_hoc_amount:r.adHocAmount}:{},variant:null===(n=r.selectedVariant)||void 0===n?void 0:n.id,...r.note?{note:r.note}:{},...r.bundleComponentVariants&&Object.keys(r.bundleComponentVariants).length?{bundle_component_variants:r.bundleComponentVariants}:{}}),toggleCart(!0),setProduct(e,{dialog:null})}catch(e){throw console.error(e),state.error=e,e}finally{setProduct(e,{busy:!1})}},getProductBuyLink=(e,t,o={})=>{var d,a,i,n,r,l,s;const c=state[e];if(c&&(null===(d=c.selectedPrice)||void 0===d?void 0:d.id)&&(!(null===(a=c.selectedPrice)||void 0===a?void 0:a.ad_hoc)||c.adHocAmount||0===c.adHocAmount))return addQueryArgs(t,{line_items:[{price:null===(i=c.selectedPrice)||void 0===i?void 0:i.id,quantity:Math.max((null===(n=c.selectedPrice)||void 0===n?void 0:n.ad_hoc)?1:c.quantity,1),...(null===(r=c.selectedPrice)||void 0===r?void 0:r.ad_hoc)?{ad_hoc_amount:c.adHocAmount}:{},...(null===(l=c.selectedVariant)||void 0===l?void 0:l.id)?{variant:null===(s=c.selectedVariant)||void 0===s?void 0:s.id}:{},...c.note?{note:c.note}:{},...c.bundleComponentVariants&&Object.keys(c.bundleComponentVariants).length?{bundle_component_variants:c.bundleComponentVariants}:{}}],...o})},getAdditionalErrorMessages=e=>((null==e?void 0:e.additional_errors)||[]).map((e=>e.message)),getTopLevelError=e=>{var t;return"checkout.invalid"===(null==e?void 0:e.code)&&(null===(t=getAdditionalErrorMessages(e))||void 0===t?void 0:t.length)?"":null==e?void 0:e.message};export{getTopLevelError as a,getAdditionalErrorMessages as b,getProductBuyLink as g,submitCartForm as s};