import { a as state } from './watchers.js';
import './watchers2.js';
import { s as state$1 } from './getters.js';
import { a as apiFetch } from './fetch.js';
import { a as addQueryArgs } from './add-query-args.js';
import { g as getQueryArg } from './get-query-arg.js';

/** The base url for this service. */
const baseUrl = 'surecart/v1/checkouts/';
/** Items to always expand. */
const expand = [
  'line_items',
  'line_item.price',
  'line_item.fees',
  'price.product',
  'customer',
  'customer.shipping_address',
  'payment_intent',
  'discount',
  'discount.promotion',
  'recommended_bumps',
  'bump.price',
  'discount.coupon',
  'shipping_address',
  'staged_payment_intents',
  'tax_identifier',
  'manual_payment_method',
];
/** Default data we send with every request. */
const withDefaultData = (data = {}) => {
  var _a, _b, _c, _d;
  return ({
    live_mode: state.mode !== 'test',
    group_key: state.groupId,
    abandoned_checkout_return_url: state.abandonedCheckoutReturnUrl || null,
    abandoned_checkout_enabled: state.abandonedCheckoutEnabled && !!state.abandonedCheckoutReturnUrl,
    metadata: {
      ...((data === null || data === void 0 ? void 0 : data.metadata) || {}),
      ...(((_a = window === null || window === void 0 ? void 0 : window.scData) === null || _a === void 0 ? void 0 : _a.page_id) && { page_id: (_b = window === null || window === void 0 ? void 0 : window.scData) === null || _b === void 0 ? void 0 : _b.page_id }),
      ...(((_c = state === null || state === void 0 ? void 0 : state.product) === null || _c === void 0 ? void 0 : _c.id) && { buy_page_product_id: (_d = state === null || state === void 0 ? void 0 : state.product) === null || _d === void 0 ? void 0 : _d.id }),
      page_url: window.location.href,
    },
    ...data,
  });
};
/** Default query we send with every request. */
const withDefaultQuery = (query = {}) => {
  var _a, _b;
  return ({
    ...(!!(state === null || state === void 0 ? void 0 : state.formId) && { form_id: state === null || state === void 0 ? void 0 : state.formId }),
    ...(!!((_a = state === null || state === void 0 ? void 0 : state.product) === null || _a === void 0 ? void 0 : _a.id) && { product_id: (_b = state === null || state === void 0 ? void 0 : state.product) === null || _b === void 0 ? void 0 : _b.id }),
    ...(!!state$1.config.stripe.paymentElement && { stage_processor_type: 'stripe' }),
    ...query,
  });
};
/** Get the checkout id  */
const findInitialCheckoutId = () => {
  var _a, _b;
  // check url first.
  const checkoutId = getQueryArg(window.location.href, 'checkout_id');
  if (!!checkoutId) {
    return checkoutId;
  }
  // check existing order.
  if ((_a = state === null || state === void 0 ? void 0 : state.checkout) === null || _a === void 0 ? void 0 : _a.id) {
    return (_b = state === null || state === void 0 ? void 0 : state.checkout) === null || _b === void 0 ? void 0 : _b.id;
  }
  // we don't have and order id.
  return null;
};
/** Parse the path with expansions. */
const parsePath = (id, endpoint = '') => {
  let path = id ? `${baseUrl}${id}` : baseUrl;
  path = `${path}${endpoint}`;
  return addQueryArgs(path, {
    expand,
  });
};
/** Fethc a checkout by id */
const fetchCheckout = async ({ id, query = {} }) => {
  return (await apiFetch({
    path: addQueryArgs(parsePath(id), withDefaultQuery(query)),
  }));
};
/** Create or update the checkout. */
const createOrUpdateCheckout = async ({ id = null, data = {}, query = {} }) => {
  id = !id ? findInitialCheckoutId() : id;
  return await apiFetch({
    method: id ? 'PATCH' : 'POST',
    path: addQueryArgs(parsePath(id), withDefaultQuery(query)),
    data: withDefaultData(data),
  });
};
/** Update the checkout. */
const updateCheckout = async ({ id, data = {}, query = {} }) => {
  return await apiFetch({
    method: 'PATCH',
    path: addQueryArgs(parsePath(id), withDefaultQuery(query)),
    data: withDefaultData(data),
  });
};
/** Finalize a checkout */
const finalizeCheckout = async ({ id, data = {}, query = {}, processor }) => {
  return (await apiFetch({
    method: 'POST',
    path: addQueryArgs(parsePath(id, '/finalize'), withDefaultQuery({
      ...((processor === null || processor === void 0 ? void 0 : processor.manual) ? { manual_payment: true, manual_payment_method_id: processor === null || processor === void 0 ? void 0 : processor.id } : { processor_type: processor === null || processor === void 0 ? void 0 : processor.id }),
      ...query,
    })),
    data: withDefaultData(data),
  }));
};

export { finalizeCheckout as a, baseUrl as b, createOrUpdateCheckout as c, expand as e, fetchCheckout as f, updateCheckout as u };
