import BigNumber from "bignumber.js";
import { TIME_INTERVALS_IN_SEC } from "./date.helpers";

export type AnyNumber = BigNumber | string | number;
export const WEI_DECIMALS_POW = 18;
export const RAY = '1e27';
export const WEEKS_IN_YEAR = 52;

export const normalizeValue = (
  value: AnyNumber = 0,
  decimals?: number | string,
  mathOps?: (value: BigNumber) => BigNumber
) => {
  let number = new BigNumber(value || 0).div(`1e${decimals || 0}`);

  if (mathOps) {
    number = mathOps(number);
  }

  return number;
};

export const absolutizeValue = (
  value: AnyNumber = 0,
  decimals?: number | string,
  mathOps?: (value: BigNumber) => BigNumber
) => normalizeValue(value, decimals && -1 * Number(decimals), mathOps);

export const convertRatePerSecToRatePerYear = (ratePerSec: AnyNumber | undefined) =>
  normalizeValue(ratePerSec, WEI_DECIMALS_POW - 2).times(TIME_INTERVALS_IN_SEC.year);
