import {calculerPourcentage, round2} from "./number.functions";
import {INumberObject} from "./types";

export const increaseInObject = (obj: INumberObject, key: string) => {
  if (obj[key]) {
    obj[key]++;
  } else {
    obj[key] = 1;
  }
  if (obj["total"] != null) {
    obj["total"]++;
  }
};

export const calculatePourcentageObj = (obj: INumberObject) => {
  const total = obj.total;

  if (typeof total !== "number") {
    throw "Erreur total";
  }

  const objPourcentages: INumberObject = {};
  delete obj.total;

  for (const key in obj) {
    objPourcentages[key] = round2(calculerPourcentage(obj[key], total));
  }

  return objPourcentages;
};
