import{getValidAdHocAmount}from"./getters";import{updateLineItem}from"./mutations";import state,{on,set}from"./store";import{onChange}from"../checkout/store";import{isInRange}from"../../functions/util";import{getLineItemByProductId}from"../checkout/getters";onChange("checkout",(()=>{Object.keys(state).forEach((o=>{var t;const e=getLineItemByProductId(o);if(e)return set(o,{...state[o],selectedPrice:e.price,ad_hoc_amount:e.ad_hoc_amount,custom_amount:(null===(t=state[o].amounts||[])||void 0===t?void 0:t.includes(e.ad_hoc_amount))?null:e.ad_hoc_amount});set(o,{...state[o],selectedPrice:null,ad_hoc_amount:null,custom_amount:null})}))})),Object.keys(state).forEach((o=>{on("set",((t,e,n)=>{var u,i,d,l,c,a;if(t===o&&((null==e?void 0:e.selectedPrice)||(null==e?void 0:e.ad_hoc_amount)||(null==e?void 0:e.custom_amount))&&((null===(u=null==e?void 0:e.selectedPrice)||void 0===u?void 0:u.id)!==(null===(i=null==n?void 0:n.selectedPrice)||void 0===i?void 0:i.id)||(null==e?void 0:e.ad_hoc_amount)!==(null==n?void 0:n.ad_hoc_amount)||(null==e?void 0:e.custom_amount)!==(null==n?void 0:n.custom_amount))){const t=(null==e?void 0:e.custom_amount)&&isInRange(null==e?void 0:e.custom_amount,e.selectedPrice)?null==e?void 0:e.custom_amount:getValidAdHocAmount(o),n=(null===(d=e.selectedPrice)||void 0===d?void 0:d.id)||(null===(a=null===(c=null===(l=e.product)||void 0===l?void 0:l.prices)||void 0===c?void 0:c.data.find((o=>null==o?void 0:o.ad_hoc)))||void 0===a?void 0:a.id);if(!n)return;updateLineItem(o,{price:n,quantity:1,ad_hoc_amount:t})}}))}));