"use strict";require("./fetch-5e8dc1d5.js");const store=require("./store-401bdb4d.js"),watchers=require("./watchers-d2dd8b04.js"),mutations=require("./mutations-d5d6ddf1.js"),index=require("./index-7ced8198.js"),addQueryArgs=require("./add-query-args-49dcb630.js"),trackOffer=()=>{var e;return index.apiFetch({path:`surecart/v1/checkouts/${store.state.checkout_id}/offer_upsell/${null===(e=store.state.upsell)||void 0===e?void 0:e.id}`,method:"POST",keepalive:!0})},preview=async()=>{try{if(!store.state.checkout_id||"busy"===store.state.loading)return;store.state.loading="busy",mutations.removeNotice();const{checkout:e,...t}=await upsellRequest({preview:!0});store.state.checkout=e,store.state.line_item=t}catch(e){if(console.error(e),((null==e?void 0:e.additional_errors)||[]).find((e=>{var t,o,i;return null===(i=null===(o=null===(t=null==e?void 0:e.data)||void 0===t?void 0:t.options)||void 0===o?void 0:o.purchasable_statuses)||void 0===i?void 0:i.includes("out_of_stock")})))return mutations.createErrorNotice({code:"out_of_stock",message:wp.i18n.__("Apologies, this is currently out of stock.","surecart")});if(((null==e?void 0:e.additional_errors)||[]).find((e=>"line_item.upsell.expired"===(null==e?void 0:e.code))))return store.state.loading="idle",mutations.createErrorNotice({code:"expired",message:wp.i18n.__("This offer has expired.","surecart")}),decline();mutations.createErrorNotice(e)}finally{store.state.loading="idle"}},accept=async()=>{try{if(!store.state.checkout_id||"busy"===store.state.loading)return;store.state.loading="busy",mutations.removeNotice();const{checkout:e}=await upsellRequest({preview:!1});handleCompletion(e)}catch(e){store.state.loading="idle",mutations.createErrorNotice(e)}},decline=async()=>{var e,t,o,i,r,s;try{if(!store.state.checkout_id||"busy"===store.state.loading)return;store.state.loading="busy",mutations.removeNotice();const d=await index.apiFetch({path:addQueryArgs.addQueryArgs(`surecart/v1/checkouts/${store.state.checkout_id}/decline_upsell/${null===(e=store.state.upsell)||void 0===e?void 0:e.id}`,{expand:["checkout","checkout.current_upsell","fees"]}),method:"POST",data:{...null===(o=watchers.state[null===(t=store.state.product)||void 0===t?void 0:t.id])||void 0===o?void 0:o.line_item,price_id:null===(r=null===(i=store.state.upsell)||void 0===i?void 0:i.price)||void 0===r?void 0:r.id,upsell:null===(s=store.state.upsell)||void 0===s?void 0:s.id,checkout:store.state.checkout_id}});handleCompletion(d)}catch(e){store.state.loading="idle",mutations.createErrorNotice(e)}},upsellRequest=e=>{var t,o,i,r,s;return index.apiFetch({path:addQueryArgs.addQueryArgs("surecart/v1/line_items/upsell",{...e,expand:["checkout","checkout.current_upsell","checkout.manual_payment_method","fees","line_item","line_item.price"]}),method:"POST",data:{...null===(o=watchers.state[null===(t=store.state.product)||void 0===t?void 0:t.id])||void 0===o?void 0:o.line_item,price_id:null===(r=null===(i=store.state.upsell)||void 0===i?void 0:i.price)||void 0===r?void 0:r.id,upsell:null===(s=store.state.upsell)||void 0===s?void 0:s.id,checkout:store.state.checkout_id}})},handleCompletion=e=>{var t,o,i,r,s;if(!(null===(t=e.current_upsell)||void 0===t?void 0:t.permalink)||(null===(o=null==e?void 0:e.current_upsell)||void 0===o?void 0:o.permalink)===(null===(i=store.state.upsell)||void 0===i?void 0:i.permalink))return store.state.loading="complete";store.state.loading="redirecting",window.location.assign(addQueryArgs.addQueryArgs(null===(r=e.current_upsell)||void 0===r?void 0:r.permalink,{sc_checkout_id:null===(s=store.state.checkout)||void 0===s?void 0:s.id,sc_form_id:store.state.form_id}))};exports.accept=accept,exports.decline=decline,exports.preview=preview,exports.trackOffer=trackOffer;