import { getQueryArg } from '@wordpress/url';
// Get only enabled price choices.
export const getEnabledPriceChoices = (choices) => {
  return (choices || []).filter(choice => (choice === null || choice === void 0 ? void 0 : choice.enabled) !== false);
};
// convert price choice to line item data.
export const convertPriceChoiceToLineItemData = (choice) => {
  return {
    price_id: choice.id,
    quantity: choice.quantity,
  };
};
export const convertLineItemsToLineItemData = (lineItems) => {
  return ((lineItems === null || lineItems === void 0 ? void 0 : lineItems.data) || []).map(item => {
    return {
      price_id: item.price.id,
      quantity: item.quantity,
    };
  });
};
export const addLineItem = (lineItems, data) => {
  const lineItemData = convertLineItemsToLineItemData(lineItems);
  const index = lineItemData.findIndex(item => item.price_id === data.price_id);
  lineItemData[index] = data;
  return lineItemData;
};
/**
 * Calculates the initial line items for the session.
 */
export const calculateInitialLineItems = (choices, choiceType) => {
  // check the url query first.
  const prices = getQueryArg(window.location.href, 'prices');
  if (prices) {
    return prices.map(convertPriceChoiceToLineItemData);
  }
  // get prices from choices.
  return getInitialChoiceLineItems(choices, choiceType);
};
/**
 * Get the initial choice line items.
 */
export const getInitialChoiceLineItems = (choices, choiceType) => {
  var _a;
  if (choiceType === 'all') {
    return getEnabledPriceChoices(choices).map(convertPriceChoiceToLineItemData);
  }
  else {
    return [(_a = getEnabledPriceChoices(choices).map(convertPriceChoiceToLineItemData)) === null || _a === void 0 ? void 0 : _a[0]];
  }
};
/**
 * Get price ids from line items
 * @param order
 * @returns
 */
export const getLineItemPriceIds = (line_items) => {
  return ((line_items === null || line_items === void 0 ? void 0 : line_items.data) || []).map(item => item.price.id);
};
export const getLineItemPrices = (line_items) => {
  return ((line_items === null || line_items === void 0 ? void 0 : line_items.data) || []).map(item => item.price);
};
export const getLineItemByPriceId = (line_items, priceId) => {
  return ((line_items === null || line_items === void 0 ? void 0 : line_items.data) || []).find(item => item.price.id === priceId);
};
/**
 * Is this product in the checkout session?
 */
export const isProductInOrder = (product, order) => {
  const prices = getLineItemPrices(order === null || order === void 0 ? void 0 : order.line_items);
  if (!(prices === null || prices === void 0 ? void 0 : prices.length))
    return false;
  return !!prices.find(price => { var _a; return ((_a = price === null || price === void 0 ? void 0 : price.product) === null || _a === void 0 ? void 0 : _a.id) === product.id; });
};
/**
 * Is the price in a checkout session
 */
export const isPriceInOrder = (price, order) => {
  const priceIds = getLineItemPriceIds(order === null || order === void 0 ? void 0 : order.line_items);
  return !!priceIds.find(id => (price === null || price === void 0 ? void 0 : price.id) === id);
};
/**
 * Attempt to get the session id
 *
 * @param formId The form id.
 * @param order The order
 * @param refresh Should we refresh?
 *
 * @returns string
 */
export const getSessionId = (formId, order, refresh = false) => {
  // if we want to get a fresh session, skip
  if (refresh === true) {
    return false;
  }
  // if we already have an ID set, return that:
  if (order === null || order === void 0 ? void 0 : order.id) {
    return order.id;
  }
  // check the url query first
  const urlId = getQueryArg(window.location.href, 'order');
  if (urlId) {
    return urlId;
  }
  // check id in localstorage
  return window.localStorage.getItem(formId);
};
/** Check if the order has a subscription */
export const hasSubscription = (order) => {
  var _a, _b, _c;
  // no line items.
  if (!((_b = (_a = order === null || order === void 0 ? void 0 : order.line_items) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.length))
    return false;
  // has subscription product.
  return (_c = order === null || order === void 0 ? void 0 : order.line_items.data) === null || _c === void 0 ? void 0 : _c.some(item => { var _a; return (_a = item === null || item === void 0 ? void 0 : item.price) === null || _a === void 0 ? void 0 : _a.recurring_interval_count; });
};
export const hasTrial = (order) => {
  return order === null || order === void 0 ? void 0 : order.trial_amount;
};
/** Check if the order has a payment plan. */
export const hasPaymentPlan = (order) => {
  var _a;
  return hasSubscription(order) && ((_a = order === null || order === void 0 ? void 0 : order.line_items.data) === null || _a === void 0 ? void 0 : _a.some(item => { var _a; return (_a = item === null || item === void 0 ? void 0 : item.price) === null || _a === void 0 ? void 0 : _a.recurring_period_count; }));
};
