import{s as i}from"./p-53e7f7d7.js";import{a as o}from"./p-dd3464cd.js";import{a as d}from"./p-401e165e.js";import{g as t}from"./p-d3366af3.js";const e="surecart/v1/checkouts/",n=["checkout_fees","shipping_fees","line_items","line_item.price","line_item.fees","line_item.variant","line_item.swap","variant.image","price.product","price.current_swap","swap.swap_price","product.product_medias","product.featured_product_media","product.product_collections","product_media.media","customer","customer.shipping_address","payment_intent","discount","discount.promotion","recommended_bumps","bump.price","current_upsell","product.variants","discount.coupon","shipping_address","billing_address","tax_identifier","manual_payment_method","shipping_choices","shipping_choice.shipping_method","invoice"],a=(d={})=>{var e,o,a,t,n,l,u;return{live_mode:"test"!==i.mode,group_key:i.groupId,abandoned_checkout_enabled:i.abandonedCheckoutEnabled,billing_matches_shipping:null===(e=i.checkout)||void 0===e?void 0:e.billing_matches_shipping,metadata:{...(null==d?void 0:d.metadata)||{},...(null===(o=null===window||void 0===window?void 0:window.scData)||void 0===o?void 0:o.page_id)&&{page_id:null===(a=null===window||void 0===window?void 0:window.scData)||void 0===a?void 0:a.page_id},...(null===(t=null==i?void 0:i.product)||void 0===t?void 0:t.id)&&{buy_page_product_id:null===(n=null==i?void 0:i.product)||void 0===n?void 0:n.id},page_url:window.location.href},...(null===(l=null==i?void 0:i.checkout)||void 0===l?void 0:l.email)&&{email:null===(u=null==i?void 0:i.checkout)||void 0===u?void 0:u.email},...d}},l=(d={})=>{var e,o,a,t;return{...!!(null==i?void 0:i.formId)&&{form_id:null==i?void 0:i.formId},...!!(null===(e=null==i?void 0:i.product)||void 0===e?void 0:e.id)&&{product_id:null===(o=null==i?void 0:i.product)||void 0===o?void 0:o.id},...!!(null===(t=null===(a=null==i?void 0:i.checkout)||void 0===a?void 0:a.invoice)||void 0===t?void 0:t.id)&&{type:"open_invoice"},...d}},u=()=>{var d,e;return t(window.location.href,"checkout_id")||((null===(d=null==i?void 0:i.checkout)||void 0===d?void 0:d.id)?null===(e=null==i?void 0:i.checkout)||void 0===e?void 0:e.id:null)},r=(i,o="")=>{let a=i?`${e}${i}`:e;return a=`${a}${o}`,d(a,{expand:n})},s=async({id:i,query:e={}})=>await o({path:d(r(i),l(e))}),v=async({id:i=null,data:e={},query:t={}})=>(i=i||u(),await o({method:i?"PATCH":"POST",path:d(r(i),l(t)),data:a(e)})),c=async({data:i={},query:e={}})=>await o({method:"POST",path:d(r(null),l(e)),data:a(i)}),p=async({id:i,data:e={},query:t={}})=>await o({method:"PATCH",path:d(r(i),l(t)),data:a(e)}),m=async({id:i,data:e={},query:t={},processor:n})=>await o({method:"POST",path:d(r(i,"/finalize"),l({...(null==n?void 0:n.manual)?{manual_payment:!0,manual_payment_method_id:null==n?void 0:n.id}:{processor_type:null==n?void 0:n.id},...t})),data:a(e)}),_=async({checkout:i,data:e,live_mode:a=!1})=>{var t;const l=((null===(t=null==i?void 0:i.line_items)||void 0===t?void 0:t.data)||[]).find((i=>{var d;return(null===(d=null==i?void 0:i.variant)||void 0===d?void 0:d.id)?i.variant.id===e.variant&&i.price.id===e.price&&i.note===e.note:i.price.id===e.price&&i.note===e.note}));if(!(null==i?void 0:i.id))return await o({method:"POST",path:d(r(null)),data:{line_items:[e],live_mode:a}});if(l)return await w({id:null==l?void 0:l.id,data:{...e,quantity:(null==l?void 0:l.quantity)+(null==e?void 0:e.quantity)}});const u=await o({path:d(`surecart/v1/line_items/${(null==l?void 0:l.id)?null==l?void 0:l.id:""}`,{consolidate:!0,expand:[...(n||[]).map((i=>i.includes(".")?i:`checkout.${i}`)),"checkout"]}),method:"POST",data:{...e,checkout:i.id}});return null==u?void 0:u.checkout},h=async({checkoutId:i,itemId:d})=>{const{deleted:e}=await o({path:`surecart/v1/line_items/${d}`,method:"DELETE"});if(!e)throw{code:"error",message:wp.i18n.__("Failed to delete","surecart")};return await s({id:i})},w=async({id:i,data:e})=>{const a=await o({path:d(`surecart/v1/line_items/${i}`,{expand:[...(n||[]).map((i=>i.includes(".")?i:`checkout.${i}`)),"checkout"]}),method:"PATCH",data:e});return null==a?void 0:a.checkout},y=async({id:i,action:e="swap"})=>{const a=await o({path:d(`surecart/v1/line_items/${i}/${e}`,{expand:[...(n||[]).map((i=>i.includes(".")?i:`checkout.${i}`)),"checkout"]}),method:"PATCH"});return null==a?void 0:a.checkout};export{_ as a,e as b,v as c,p as d,n as e,m as f,s as g,c as h,h as r,y as t,w as u};