import{getQueryArg}from"@wordpress/url";const byPosition=(i=[])=>[...i].sort(((i,n)=>{var e,t;return(null!==(e=null==i?void 0:i.position)&&void 0!==e?e:0)-(null!==(t=null==n?void 0:n.position)&&void 0!==t?t:0)}));export const getPerBundleQuantity=(i,n=1)=>{const e=Math.max(Number(n)||1,1),t=Math.max(Number(null==i?void 0:i.quantity)||1,1);return Math.max(Math.round(t/e),1)};export const getBundleComponentRowsFromLineItems=(i=[],n=1,e=!0,t="·")=>{const o=(t||"·").trim()||"·";return(i||[]).map((i=>{var t;const r=((null==i?void 0:i.variant_options)||[]).filter(Boolean),l=r.length?r.join(` ${o} `):((null==i?void 0:i.variant_display_options)||"").trim();if(!e&&!l)return null;const d=null===(t=null==i?void 0:i.price)||void 0===t?void 0:t.product,u=(null==d?void 0:d.name)||"";if(!u)return null;const a=getPerBundleQuantity(i,n);return{id:null==i?void 0:i.id,name:u,variants:l,qty:a}})).filter(Boolean)};export const getBundleComponentRowsFromBundleItems=(i=[])=>byPosition(i).map((i=>{const n=null==i?void 0:i.component_product,e=(null==n?void 0:n.name)||"",t=Math.max(Number(null==i?void 0:i.quantity)||1,1);return e?{id:null==i?void 0:i.id,name:e,variants:"",qty:t}:null})).filter(Boolean);export const groupBundleLineItems=(i=[])=>{const n=[],e=[],t={};return(i||[]).forEach((i=>{var o,r;if(null==i?void 0:i.component_line_item)return;const l=null===(o=null==i?void 0:i.price)||void 0===o?void 0:o.product;if(null==l?void 0:l.bundle)return e.push(i),void(t[i.id]=byPosition((null===(r=null==i?void 0:i.component_line_items)||void 0===r?void 0:r.data)||[]));n.push(i)})),{regular:n,bundleParents:e,componentsByParent:t}};export const getEnabledPriceChoices=i=>(i||[]).filter((i=>!1!==(null==i?void 0:i.enabled)));export const convertPriceChoiceToLineItemData=i=>({price_id:i.id,quantity:i.quantity});export const convertLineItemsToLineItemData=i=>((null==i?void 0:i.data)||[]).filter((i=>!(null==i?void 0:i.component_line_item))).map((i=>{var n;return{...(null==i?void 0:i.id)?{id:i.id}:{},price_id:i.price.id,quantity:i.quantity,variant_id:null===(n=i.variant)||void 0===n?void 0:n.id}}));export const addLineItem=(i,n)=>{const e=convertLineItemsToLineItemData(i),t=e.findIndex((i=>i.price_id===n.price_id));return e[t]=n,e};export const calculateInitialLineItems=(i,n)=>{const e=getQueryArg(window.location.href,"prices");return e?e.map(convertPriceChoiceToLineItemData):getInitialChoiceLineItems(i,n)};export const getInitialChoiceLineItems=(i,n)=>{var e;return"all"===n?getEnabledPriceChoices(i).map(convertPriceChoiceToLineItemData):[null===(e=getEnabledPriceChoices(i).map(convertPriceChoiceToLineItemData))||void 0===e?void 0:e[0]]};export const getLineItemPriceIds=i=>((null==i?void 0:i.data)||[]).map((i=>i.price.id));export const getLineItemBumpIds=i=>((null==i?void 0:i.data)||[]).map((i=>{var n;return(null===(n=null==i?void 0:i.bump)||void 0===n?void 0:n.id)||(null==i?void 0:i.bump)}));export const getLineItemPrices=i=>((null==i?void 0:i.data)||[]).map((i=>i.price));export const getLineItemByPriceId=(i,n)=>((null==i?void 0:i.data)||[]).find((i=>i.price.id===n));export const isProductInOrder=(i,n)=>{const e=getLineItemPrices(null==n?void 0:n.line_items);return!!(null==e?void 0:e.length)&&!!e.find((n=>{var e;return(null===(e=null==n?void 0:n.product)||void 0===e?void 0:e.id)===i.id}))};export const isPriceInOrder=(i,n)=>!!getLineItemPriceIds(null==n?void 0:n.line_items).find((n=>(null==i?void 0:i.id)===n));export const isBumpInOrder=(i,n)=>!!getLineItemBumpIds(null==n?void 0:n.line_items).find((n=>(null==i?void 0:i.id)===n));export const getSessionId=(i,n,e=!1)=>{if(!0===e)return!1;if(null==n?void 0:n.id)return n.id;return getQueryArg(window.location.href,"order")||window.localStorage.getItem(i)};export const hasSubscription=i=>{var n,e,t;return!!(null===(e=null===(n=null==i?void 0:i.line_items)||void 0===n?void 0:n.data)||void 0===e?void 0:e.length)&&(null===(t=null==i?void 0:i.line_items.data)||void 0===t?void 0:t.some((i=>{var n;return null===(n=null==i?void 0:i.price)||void 0===n?void 0:n.recurring_interval_count})))};export const hasTrial=i=>null==i?void 0:i.trial_amount;export const hasPaymentPlan=i=>{var n;return hasSubscription(i)&&(null===(n=null==i?void 0:i.line_items.data)||void 0===n?void 0:n.some((i=>{var n;return null===(n=null==i?void 0:i.price)||void 0===n?void 0:n.recurring_period_count})))};