import { a as apiFetch } from './fetch.js';
import { a as addQueryArgs } from './add-query-args.js';

const baseUrl = 'surecart/v1/checkouts/';
const expand = [
  'line_items',
  'line_item.price',
  'price.product',
  'customer',
  'customer.shipping_address',
  'payment_intent',
  'discount',
  'discount.promotion',
  'discount.coupon',
  'shipping_address',
  'tax_identifier',
];
const parsePath = (id, endpoint = '') => {
  let path = id ? `${baseUrl}${id}` : baseUrl;
  path = `${path}${endpoint}`;
  return addQueryArgs(path, {
    expand,
  });
};
const createOrUpdateOrder = async ({ id, data = {}, query = {} }) => {
  return await apiFetch({
    method: id ? 'PATCH' : 'POST',
    path: addQueryArgs(parsePath(id), query),
    data,
  });
};
const updateOrder = async ({ id, data = {}, query = {} }) => {
  return await apiFetch({
    method: 'PATCH',
    path: addQueryArgs(parsePath(id), query),
    data,
  });
};
/**
 * Finalize a checkout session
 */
const finalizeSession = async ({ id, data = {}, query = {}, processor }) => {
  return (await apiFetch({
    method: 'POST',
    path: addQueryArgs(parsePath(id, `/finalize/`), {
      processor_type: processor,
      ...query,
    }),
    data,
  }));
};

export { baseUrl as b, createOrUpdateOrder as c, expand as e, finalizeSession as f, updateOrder as u };
