import"./p-445fd8b4.js";import{s as o}from"./p-71d77b7d.js";import{s as e}from"./p-b3860daf.js";import{r as i,c as l}from"./p-78468e45.js";import{a as t}from"./p-259c3935.js";import{a as d}from"./p-401e165e.js";const u=()=>{var i;return t({path:`surecart/v1/checkouts/${o.checkout_id}/offer_upsell/${null===(i=o.upsell)||void 0===i?void 0:i.id}`,method:"POST",keepalive:!0})},r=async()=>{try{if(!o.checkout_id||"busy"===o.loading)return;o.loading="busy",i();const{checkout:e,...l}=await n({preview:!0});o.checkout=e,o.line_item=l}catch(i){if(console.error(i),((null==i?void 0:i.additional_errors)||[]).find((o=>{var i,e,l;return null===(l=null===(e=null===(i=null==o?void 0:o.data)||void 0===i?void 0:i.options)||void 0===e?void 0:e.purchasable_statuses)||void 0===l?void 0:l.includes("out_of_stock")})))return l({code:"out_of_stock",message:wp.i18n.__("Apologies, this is currently out of stock.","surecart")});if(((null==i?void 0:i.additional_errors)||[]).find((o=>"line_item.upsell.expired"===(null==o?void 0:o.code))))return o.loading="idle",l({code:"expired",message:wp.i18n.__("This offer has expired.","surecart")}),c();l(i)}finally{o.loading="idle"}},s=async()=>{try{if(!o.checkout_id||"busy"===o.loading)return;o.loading="busy",i();const{checkout:e}=await n({preview:!1});v(e)}catch(i){o.loading="idle",l(i)}},c=async()=>{var u,r,c,n,s,a;try{if(!o.checkout_id||"busy"===o.loading)return;o.loading="busy",i();const l=await t({path:d(`surecart/v1/checkouts/${o.checkout_id}/decline_upsell/${null===(u=o.upsell)||void 0===u?void 0:u.id}`,{expand:["checkout","checkout.current_upsell","fees"]}),method:"POST",data:{...null===(c=e[null===(r=o.product)||void 0===r?void 0:r.id])||void 0===c?void 0:c.line_item,price_id:null===(s=null===(n=o.upsell)||void 0===n?void 0:n.price)||void 0===s?void 0:s.id,upsell:null===(a=o.upsell)||void 0===a?void 0:a.id,checkout:o.checkout_id}});v(l)}catch(i){o.loading="idle",l(i)}},n=i=>{var l,u,r,c,n;return t({path:d("surecart/v1/line_items/upsell",{...i,expand:["checkout","checkout.current_upsell","checkout.manual_payment_method","fees","line_item","line_item.price"]}),method:"POST",data:{...null===(u=e[null===(l=o.product)||void 0===l?void 0:l.id])||void 0===u?void 0:u.line_item,price_id:null===(c=null===(r=o.upsell)||void 0===r?void 0:r.price)||void 0===c?void 0:c.id,upsell:null===(n=o.upsell)||void 0===n?void 0:n.id,checkout:o.checkout_id}})},v=i=>{var e,l,t,u,r;if(!(null===(e=i.current_upsell)||void 0===e?void 0:e.permalink)||(null===(l=null==i?void 0:i.current_upsell)||void 0===l?void 0:l.permalink)===(null===(t=o.upsell)||void 0===t?void 0:t.permalink))return o.loading="complete";o.loading="redirecting",window.location.assign(d(null===(u=i.current_upsell)||void 0===u?void 0:u.permalink,{sc_checkout_id:null===(r=o.checkout)||void 0===r?void 0:r.id,sc_form_id:o.form_id}))};export{s as a,c as d,r as p,u as t};