const  PERCENTAGE_SIGN = '%';
const  PERCENTAGE = 'percentage';
const  NUMBER = 'number';

const percentageHandler = (number, decimals) => {
  let value = Number((parseFloat(number) / 100));
  if (decimals) {
    return value.toFixed(decimals);
  }
  return value;
}

export function numberHandler({ type = NUMBER , decimals }) {
  switch (type.toLowerCase()) {
    case PERCENTAGE:
      return {
        handler: ({ value }) => {
          if (Array.isArray(value)) {
            return value.map(val => percentageHandler(val, decimals));
          }
          return percentageHandler(value, decimals);
        },
        unit: PERCENTAGE_SIGN,
      }
      break;
    default:
      return {
        handler: ({ value }) => {
          if (Array.isArray(value)) {
            return value.map(Number);
          }
          return Number(value);
        },
        unit: '',
      }
      break;
  }
}