import{s as state}from"./mutations-83ea96b2.js";import{a as apiFetch}from"./fetch-2d92a73e.js";import{a as addQueryArgs}from"./add-query-args-0e2a8393.js";import{g as getQueryArg}from"./remove-query-args-938c53ea.js";const baseUrl="surecart/v1/checkouts/",expand=["line_items","line_item.price","line_item.fees","line_item.variant","variant.image","price.product","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"],withDefaultData=(e={})=>{var t,a,i,d,o,u,n;return{live_mode:"test"!==state.mode,group_key:state.groupId,abandoned_checkout_enabled:state.abandonedCheckoutEnabled,billing_matches_shipping:null===(t=state.checkout)||void 0===t?void 0:t.billing_matches_shipping,metadata:{...(null==e?void 0:e.metadata)||{},...(null===(a=null===window||void 0===window?void 0:window.scData)||void 0===a?void 0:a.page_id)&&{page_id:null===(i=null===window||void 0===window?void 0:window.scData)||void 0===i?void 0:i.page_id},...(null===(d=null==state?void 0:state.product)||void 0===d?void 0:d.id)&&{buy_page_product_id:null===(o=null==state?void 0:state.product)||void 0===o?void 0:o.id},page_url:window.location.href},...(null===(u=null==state?void 0:state.checkout)||void 0===u?void 0:u.email)&&{email:null===(n=null==state?void 0:state.checkout)||void 0===n?void 0:n.email},...e}},withDefaultQuery=(e={})=>{var t,a,i,d;return{...!!(null==state?void 0:state.formId)&&{form_id:null==state?void 0:state.formId},...!!(null===(t=null==state?void 0:state.product)||void 0===t?void 0:t.id)&&{product_id:null===(a=null==state?void 0:state.product)||void 0===a?void 0:a.id},...!!(null===(d=null===(i=null==state?void 0:state.checkout)||void 0===i?void 0:i.invoice)||void 0===d?void 0:d.id)&&{type:"open_invoice"},...e}},findInitialCheckoutId=()=>{var e,t;return getQueryArg(window.location.href,"checkout_id")||((null===(e=null==state?void 0:state.checkout)||void 0===e?void 0:e.id)?null===(t=null==state?void 0:state.checkout)||void 0===t?void 0:t.id:null)},parsePath=(e,t="")=>{let a=e?`${baseUrl}${e}`:baseUrl;return a=`${a}${t}`,addQueryArgs(a,{expand:expand})},fetchCheckout=async({id:e,query:t={}})=>await apiFetch({path:addQueryArgs(parsePath(e),withDefaultQuery(t))}),createOrUpdateCheckout=async({id:e=null,data:t={},query:a={}})=>(e=e||findInitialCheckoutId(),await apiFetch({method:e?"PATCH":"POST",path:addQueryArgs(parsePath(e),withDefaultQuery(a)),data:withDefaultData(t)})),createCheckout=async({data:e={},query:t={}})=>await apiFetch({method:"POST",path:addQueryArgs(parsePath(null),withDefaultQuery(t)),data:withDefaultData(e)}),updateCheckout=async({id:e,data:t={},query:a={}})=>await apiFetch({method:"PATCH",path:addQueryArgs(parsePath(e),withDefaultQuery(a)),data:withDefaultData(t)}),finalizeCheckout=async({id:e,data:t={},query:a={},processor:i})=>await apiFetch({method:"POST",path:addQueryArgs(parsePath(e,"/finalize"),withDefaultQuery({...(null==i?void 0:i.manual)?{manual_payment:!0,manual_payment_method_id:null==i?void 0:i.id}:{processor_type:null==i?void 0:i.id},...a})),data:withDefaultData(t)}),addLineItem=async({checkout:e,data:t,live_mode:a=!1})=>{var i;const d=((null===(i=null==e?void 0:e.line_items)||void 0===i?void 0:i.data)||[]).find((e=>{var a;return(null===(a=null==e?void 0:e.variant)||void 0===a?void 0:a.id)?e.variant.id===t.variant&&e.price.id===t.price:e.price.id===t.price}));if(!(null==e?void 0:e.id))return await apiFetch({method:"POST",path:addQueryArgs(parsePath(null)),data:{line_items:[t],live_mode:a}});if(d)return await updateLineItem({id:null==d?void 0:d.id,data:{...t,quantity:(null==d?void 0:d.quantity)+(null==t?void 0:t.quantity)}});const o=await apiFetch({path:addQueryArgs(`surecart/v1/line_items/${(null==d?void 0:d.id)?null==d?void 0:d.id:""}`,{consolidate:!0,expand:[...(expand||[]).map((e=>e.includes(".")?e:`checkout.${e}`)),"checkout"]}),method:"POST",data:{...t,checkout:e.id}});return null==o?void 0:o.checkout},removeLineItem=async({checkoutId:e,itemId:t})=>{const{deleted:a}=await apiFetch({path:`surecart/v1/line_items/${t}`,method:"DELETE"});if(!a)throw{code:"error",message:wp.i18n.__("Failed to delete","surecart")};return await fetchCheckout({id:e})},updateLineItem=async({id:e,data:t})=>{const a=await apiFetch({path:addQueryArgs(`surecart/v1/line_items/${e}`,{expand:[...(expand||[]).map((e=>e.includes(".")?e:`checkout.${e}`)),"checkout"]}),method:"PATCH",data:t});return null==a?void 0:a.checkout};export{addLineItem as a,baseUrl as b,createOrUpdateCheckout as c,updateCheckout as d,expand as e,finalizeCheckout as f,fetchCheckout as g,createCheckout as h,removeLineItem as r,updateLineItem as u};