{"size": 1728, "ext": "js", "max_stars_repo_path": "src/actions.js", "max_stars_repo_name": "kewitz/splitter", "max_stars_repo_stars_event_min_datetime": "2016-01-30T20:09:39.000Z", "max_stars_repo_stars_event_max_datetime": "2020-04-14T15:18:37.000Z", "max_issues_repo_path": "src/actions.js", "max_issues_repo_name": "kewitz/splitter", "max_issues_repo_issues_event_min_datetime": "2016-02-20T19:04:40.000Z", "max_issues_repo_issues_event_max_datetime": "2017-01-13T21:44:19.000Z", "max_forks_repo_path": "src/actions.js", "max_forks_repo_name": "kewitz/splitter", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 27.0, "max_line_length": 102, "alphanum_fraction": 0.603587963}
let paymentIds = []
export function addPayment(value) {
  const i = paymentIds.slice(-1)[0]+1 || 0;
  paymentIds.push(i);
  return {
    id: i,
    type: 'PAYMENT_ADD',
    value
  }
}

export function delPayment(id) {
  paymentIds = paymentIds.filter(i => i != id);
  return {
    type: 'PAYMENT_DEL',
    id
  }
}

export function makeColors(index) {
  const step = 360/paymentIds.length;
  const deg = step*paymentIds.indexOf(index);
  return "hsl("+deg+", 80%, 70%)";
}

export function solveActions(payments) {
  // Calcula a conta final, média e balanço de cada pessoa.
  const sum = payments.reduce((sum, cur) => sum + cur.value, 0);
  const avg = sum / payments.length;
  let balances = payments
    .map(p => ({ ...p, value: (avg - p.value) }))
    .filter(b => b.value != 0)
    .sort((a, b) => a.value < b.value);

  let actions = [];

  while(balances.filter(b => b.value.toFixed(2, 0) != 0).length > 0) {
    const payer = balances.slice(0,1)[0];
    const receiver = balances.slice(-1)[0];
    const action = {
      from: payer.id,
      to: receiver.id,
      amount: Math.min(payer.value, Math.abs(receiver.value)),
    };

    balances = balances
      .map(balance => {
        if (balance.id == action.from) return { ...balance, value: balance.value - action.amount };
        else if (balance.id == action.to) return { ...balance, value: balance.value + action.amount };
        else return balance;
      })
      .filter(b => b.value != 0.00);

    if (balances.length) {
      const lastReceiver = balances.slice(-1)[0];
      if (lastReceiver.value > 0) balances = [lastReceiver, ...balances.slice(0,-1)];
    }

    actions.push(action);
  }
  console.log(JSON.stringify(actions));
  return actions;
}
