import { convertPrice } from '../../lib/tools';

export const getPrice = (
  supplierCurrencyId,
  leaseObjectPrice,
  calculationStore,
) => {
  const supplierCurrency = calculationStore
    .getOptions('selectSupplierCurrency')
    .find(x => x.transactioncurrencyid === supplierCurrencyId);
  const evo_currencychange = calculationStore
    .getStaticData('evo_currencychange')
    .find(x => x.evo_ref_transactioncurrency === supplierCurrencyId);
  const evo_currencychangeValue =
    (evo_currencychange && evo_currencychange.evo_currencychange) || 0;
  let price = convertPrice(
    supplierCurrency.isocurrencycode,
    parseFloat(leaseObjectPrice),
    evo_currencychangeValue,
  );
  return price;
};

export const calculatePerc = calculationStore => (
  supplierCurrencyId,
  leaseObjectPrice,
  rubSum,
) => {
  const price = getPrice(
    supplierCurrencyId,
    leaseObjectPrice,
    calculationStore,
  );
  return (rubSum / price) * 100;
};

export const calculateRub = calculationStore => (
  supplierCurrencyId,
  leaseObjectPrice,
  perc,
) => {
  const price = getPrice(
    supplierCurrencyId,
    leaseObjectPrice,
    calculationStore,
  );
  return (perc * price) / 100;
};
