// regex matches 1 or more digits or decimals, optional whitespace, "bpm" with any casing
const getBPMTerms = (str: string) => str.match(/([\d.]+\s*?bpm)/gi) || [];

export const parseSampleQuery = (str: string) => {
  const bpmTerms = getBPMTerms(str);

  // in the future, `const keyTerms ... ` etc
  let strWithoutBPMTerms = (bpmTerms as string[]).reduce((acc, term) => acc.replace(term, ''), str);

  return {
    bpm: bpmTerms[0] ? Number(bpmTerms[0].replace(/bpm/i, '').trim()) : null,
    text: strWithoutBPMTerms.trim().replace(/\s+/g, ' '), // trim and remove double-spaces resulting from removed terms
  };
};
