JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3Rbr{ gilour

File "find-best-price.ts"

Full Path: /home/markqprx/iniasli.pro/client/billing/pricing-table/find-best-price.ts
File size: 966 bytes
MIME-type: text/plain
Charset: utf-8

import {Price} from '../price';

export type UpsellBillingCycle = 'monthly' | 'yearly';

export function findBestPrice(
  token: UpsellBillingCycle,
  prices: Price[]
): Price | undefined {
  if (token === 'monthly') {
    const match = findMonthlyPrice(prices);
    if (match) return match;
  }

  if (token === 'yearly') {
    const match = findYearlyPrice(prices);
    if (match) return match;
  }

  return prices[0];
}

function findYearlyPrice(prices: Price[]) {
  return prices.find(price => {
    if (price.interval === 'month' && price.interval_count >= 12) {
      return price;
    }
    if (price.interval === 'year' && price.interval_count >= 1) {
      return price;
    }
  });
}

function findMonthlyPrice(prices: Price[]) {
  return prices.find(price => {
    if (price.interval === 'day' && price.interval_count >= 30) {
      return price;
    }
    if (price.interval === 'month' && price.interval_count >= 1) {
      return price;
    }
  });
}