import{s as i}from"./p-b28405ea.js";import"./p-8dccb656.js";import{a as o}from"./p-401e165e.js";import{g as d}from"./p-d3366af3.js";import{a as t}from"./p-259c3935.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","line_item.component_line_items"],a=(e={})=>{var o,t,d,a,n,l,u;return{live_mode:"test"!==i.mode,group_key:i.groupId,abandoned_checkout_enabled:i.abandonedCheckoutEnabled,billing_matches_shipping:null===(o=i.checkout)||void 0===o?void 0:o.billing_matches_shipping,metadata:{...(null==e?void 0:e.metadata)||{},...(null===(t=null===window||void 0===window?void 0:window.scData)||void 0===t?void 0:t.page_id)&&{page_id:null===(d=null===window||void 0===window?void 0:window.scData)||void 0===d?void 0:d.page_id},...(null===(a=null==i?void 0:i.product)||void 0===a?void 0:a.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},...i.geoCoordinates||{},...e}},l=(e={})=>{var o,t,d,a;return{...!!(null==i?void 0:i.formId)&&{form_id:null==i?void 0:i.formId},...!!(null===(o=null==i?void 0:i.product)||void 0===o?void 0:o.id)&&{product_id:null===(t=null==i?void 0:i.product)||void 0===t?void 0:t.id},...!!(null===(a=null===(d=null==i?void 0:i.checkout)||void 0===d?void 0:d.invoice)||void 0===a?void 0:a.id)&&{type:"open_invoice"},...e}},u=()=>{var e,o;return d(window.location.href,"checkout_id")||((null===(e=null==i?void 0:i.checkout)||void 0===e?void 0:e.id)?null===(o=null==i?void 0:i.checkout)||void 0===o?void 0:o.id:null)},r=(i,t="")=>{let d=i?`${e}${i}`:e;return d=`${d}${t}`,o(d,{expand:n})},s=async({id:i,query:e={}})=>await t({path:o(r(i),l(e))}),c=async({id:i=null,data:e={},query:d={}})=>(i=i||u(),await t({method:i?"PATCH":"POST",path:o(r(i),l(d)),data:a(e)})),v=async({data:i={},query:e={}})=>await t({method:"POST",path:o(r(null),l(e)),data:a(i)}),p=async({id:i,data:e={},query:d={}})=>await t({method:"PATCH",path:o(r(i),l(d)),data:a(e)}),m=async({id:i,data:e={},query:d={},processor:n})=>await t({method:"POST",path:o(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},...d})),data:a(e)}),_=async({checkout:i,data:e,live_mode:d=!1})=>{var a;const l=((null===(a=null==i?void 0:i.line_items)||void 0===a?void 0:a.data)||[]).find((i=>{var o;return(null===(o=null==i?void 0:i.variant)||void 0===o?void 0:o.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 t({method:"POST",path:o(r(null)),data:{line_items:[e],live_mode:d}});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 t({path:o(`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:e})=>{const{deleted:o}=await t({path:`surecart/v1/line_items/${e}`,method:"DELETE"});if(!o)throw{code:"error",message:wp.i18n.__("Failed to delete","surecart")};return await s({id:i})},w=async({id:i,data:e})=>{const d=await t({path:o(`surecart/v1/line_items/${i}`,{expand:[...(n||[]).map((i=>i.includes(".")?i:`checkout.${i}`)),"checkout"]}),method:"PATCH",data:e});return null==d?void 0:d.checkout},y=async({id:i,action:e="swap"})=>{const d=await t({path:o(`surecart/v1/line_items/${i}/${e}`,{expand:[...(n||[]).map((i=>i.includes(".")?i:`checkout.${i}`)),"checkout"]}),method:"PATCH"});return null==d?void 0:d.checkout};export{_ as a,e as b,c,p as d,n as e,m as f,s as g,v as h,h as r,y as t,w as u};