import state from"./store";import{getSerializedState,isProductVariantOptionMissing,isProductVariantOptionSoldOut}from"../utils";import{getVariantFromValues}from"../../functions/util";export const availablePrices=t=>{var i;return((null===(i=state[t])||void 0===i?void 0:i.prices)||[]).filter((t=>!(null==t?void 0:t.archived))).sort(((t,i)=>(null==t?void 0:t.position)-(null==i?void 0:i.position)))};export const getProduct=t=>{var i;return null!==(i=state[t])&&void 0!==i?i:null};export const isStockNeedsToBeChecked=t=>{var i,o,l,n;return!(!(null===(o=null===(i=null==state?void 0:state[t])||void 0===i?void 0:i.product)||void 0===o?void 0:o.stock_enabled)||(null===(n=null===(l=null==state?void 0:state[t])||void 0===l?void 0:l.product)||void 0===n?void 0:n.allow_out_of_stock_purchases))};export const isOptionSoldOut=(t,i,o)=>isProductVariantOptionSoldOut(i,o,state[t].variantValues,state[t].product);export const isOptionMissing=(t,i,o)=>isProductVariantOptionMissing(i,o,state[t].variantValues,state[t].product);export const isProductOutOfStock=t=>{var i,o,l,n;return!!isStockNeedsToBeChecked(t)&&((null===(o=null===(i=null==state?void 0:state[t])||void 0===i?void 0:i.selectedVariant)||void 0===o?void 0:o.id)?(null===(n=state[t].selectedVariant)||void 0===n?void 0:n.available_stock)<=0:(null===(l=state[t].product)||void 0===l?void 0:l.available_stock)<=0)};export const isSelectedVariantMissing=t=>{var i,o,l;return!!(null===(o=null===(i=null==state?void 0:state[t])||void 0===i?void 0:i.variants)||void 0===o?void 0:o.length)&&void 0===(null===(l=getVariantFromValues({variants:state[t].variants,values:state[t].variantValues}))||void 0===l?void 0:l.id)};export const getDefaultState=()=>{const{product:t={}}=getSerializedState();return Object.values(t).reduce(((t,i)=>{const{selectedPrice:o,product:l,selectedVariant:n}=i||{},e={...i,quantity:1,total:null,dialog:null,busy:!1,error:null,adHocAmount:(null==o?void 0:o.amount)||null,disabled:(null==o?void 0:o.archived)||(null==l?void 0:l.archived),line_item:{price_id:null==o?void 0:o.id,quantity:1,...(null==o?void 0:o.ad_hoc)?{ad_hoc_amount:null==o?void 0:o.amount}:{},...(null==n?void 0:n.id)?{variant_id:null==n?void 0:n.id}:{}},variantValues:{...(null==n?void 0:n.option_1)?{option_1:null==n?void 0:n.option_1}:{},...(null==n?void 0:n.option_2)?{option_2:null==n?void 0:n.option_2}:{},...(null==n?void 0:n.option_3)?{option_3:null==n?void 0:n.option_3}:{}}};return t[l.id]=e,t}),{})||{}};export const availableSubscriptionPrices=t=>(availablePrices(t)||[]).filter((t=>null==t?void 0:t.recurring_interval)).sort(((t,i)=>(null==t?void 0:t.position)-(null==i?void 0:i.position)));export const availableNonSubscriptionPrices=t=>(availablePrices(t)||[]).filter((t=>!(null==t?void 0:t.recurring_interval))).sort(((t,i)=>(null==t?void 0:t.position)-(null==i?void 0:i.position)));