import { syllables } from "./data"
import { BtnColor, SylPart, Syllable } from "./types"

export const isEnabled = (arr: SylPart[], find: string): boolean => arr.some((str) => str.index==find)

export const toggle = ( arr: SylPart[], part: SylPart ):SylPart[] => {
    if ( arr.some((btn) => btn.index==part.index) )
      return arr.filter((el)=>el.index!==part.index)
    else
      return [ ...arr, part]
}

export const genrateRandomNumber = (min: number, max: number):number => {
  min = Math.ceil(min)
  max = Math.floor(max)
  const rnd =  Math.floor(Math.random() * (max - min + 1)) + min
  return rnd
}

export const getRandomArray = <T>(fromArray:T[], count:number):T[] => {
  let _tone:T[] = []
  if (fromArray.length==0) {
    return _tone
  }
  for( let x=0; x<count; x++) {
    const randomN = genrateRandomNumber(0,fromArray.length-1)
    _tone = [..._tone, fromArray[randomN]]
  }
  return _tone
}

export const GetSyllablesByInitAndFin = (initiales:SylPart[], finales: SylPart[]):Syllable[] => {
  const inits = initiales.map( (i) => i.index)
  const fins = finales.map( (i) =>i.index )
  return syllables.filter( syl => inits.includes(syl.initiale) && fins.includes(syl.finale) )
  }

export const Outlined = (color: BtnColor):string => `outline-${color}`
export const getAudioPath = (index: string):string => `/audio/${index}.mp3`

export const getAudio = (index: string):HTMLAudioElement => new Audio(getAudioPath(index))