const average = (values) => {
  const sum = values.reduce((previous, current) => previous + current, 0);
  return sum / values.length;
};
export default (groupSize) => {
  console.log("Starting average action timer middleware");
  console.log(`Will take an average every ${groupSize} actions`);
  const bucket = {};
  return () => (next) => (action) => {
    const start = performance.now();
    const result = next(action);
    const end = performance.now();
    const duration = end - start;
    if (!bucket[action.type]) {
      bucket[action.type] = [duration];
      return result;
    }
    bucket[action.type].push(duration);
    if (bucket[action.type].length < groupSize) {
      return result;
    }
    console.warn(`Average time for ${action.type}`, average(bucket[action.type]));

    // reset
    bucket[action.type] = [];
    return result;
  };
};
