'use strict';

const fetch = require('./fetch-a8709225.js');
const addQueryArgs = require('./add-query-args-17c551b6.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.addQueryArgs(path, {
    expand,
  });
};
const createOrUpdateOrder = async ({ id, data = {}, query = {} }) => {
  return await fetch.apiFetch({
    method: id ? 'PATCH' : 'POST',
    path: addQueryArgs.addQueryArgs(parsePath(id), query),
    data,
  });
};
const updateOrder = async ({ id, data = {}, query = {} }) => {
  return await fetch.apiFetch({
    method: 'PATCH',
    path: addQueryArgs.addQueryArgs(parsePath(id), query),
    data,
  });
};
/**
 * Finalize a checkout session
 */
const finalizeSession = async ({ id, data = {}, query = {}, processor }) => {
  return (await fetch.apiFetch({
    method: 'POST',
    path: addQueryArgs.addQueryArgs(parsePath(id, `/finalize/`), {
      processor_type: processor,
      ...query,
    }),
    data,
  }));
};

exports.baseUrl = baseUrl;
exports.createOrUpdateOrder = createOrUpdateOrder;
exports.expand = expand;
exports.finalizeSession = finalizeSession;
exports.updateOrder = updateOrder;
