var _a;
import { createLocalStore } from './local';
import { createStore } from '@stencil/store';
import { state as checkoutState } from '@store/checkout';
import { addQueryArgs, removeQueryArgs } from '@wordpress/url';
const store = ((_a = window === null || window === void 0 ? void 0 : window.scData) === null || _a === void 0 ? void 0 : _a.do_not_persist_cart)
  ? createStore({
    live: {},
    test: {},
  })
  : createLocalStore('surecart-local-storage', {
    live: {},
    test: {},
  }, true);
window.scStore = store;
export default store;
/** Get the checkout. */
export const getOrder = (formId, mode) => { var _a; return ((_a = store.state[mode]) === null || _a === void 0 ? void 0 : _a[formId]) || {}; };
export const getCheckout = getOrder;
/** Set the checkout. */
export const setOrder = (data, formId) => {
  var _a;
  const mode = (data === null || data === void 0 ? void 0 : data.live_mode) ? 'live' : 'test';
  store.set(mode, { ...store.state[mode], [formId]: data });
  // update the current checkout state.
  if (checkoutState.formId === formId && checkoutState.mode === mode) {
    checkoutState.checkout = data;
  }
  // set in url only if we are not persisting the cart.
  if (((_a = window === null || window === void 0 ? void 0 : window.scData) === null || _a === void 0 ? void 0 : _a.do_not_persist_cart) && (data === null || data === void 0 ? void 0 : data.id)) {
    window.history.replaceState({}, document.title, addQueryArgs(window.location.href, { checkout_id: data === null || data === void 0 ? void 0 : data.id }));
  }
};
export const setCheckout = setOrder;
/** Clear the order from the store. */
export const clearOrder = (formId, mode) => {
  const { [formId]: remove, ...checkouts } = store.state[mode];
  window.history.replaceState({}, document.title, removeQueryArgs(window.location.href, 'redirect_status', 'coupon', 'line_items', 'confirm_checkout_id', 'checkout_id'));
  return store.set(mode, checkouts);
};
export const clearCheckout = clearOrder;
