import { Note } from './types';

const looksLikeDrums = (notes: Note[]) => {
  const CORE_KIT_THRESHOLD = 0.666;

  const pitchCounts: {[key: number]: number} = {};
  notes.forEach((n) => {
    if (!pitchCounts[n.note]) pitchCounts[n.note] = 0;
    pitchCounts[n.note] ++;
  });

  const mainPartOfGeneralMIDIDrums = notes.filter((n) => n.note >= 35 && n.note <= 51).length;

  if (mainPartOfGeneralMIDIDrums === notes.length) {
    return true;
  }

  const kickCount = pitchCounts[35] + pitchCounts[36];
  const snareAndClapCount = pitchCounts[38] + pitchCounts[39] + pitchCounts[40];
  const hihatCount = pitchCounts[42] + pitchCounts[44] + pitchCounts[46];

  if ((kickCount + snareAndClapCount + hihatCount) / notes.length > CORE_KIT_THRESHOLD) {
    return true;
  }

  if (Object.keys(pitchCounts).length <= 10 && notes.length >= 200) {
    return true;
  }

  return false;
}

export default looksLikeDrums;
