import WebFont from 'webfontloader'

export default function(state = [], action) {
  switch (action.type) {
    case 'FONT-LIST':
      let newState = action.list

      let googles = newState.filter(font => font.provider === 'google')
      if (googles.length > 0) {
        WebFont.load({
          google: {
            families: googles.map(font => font.name)
          }
        })
      }

      return newState

    case 'CLEAR-FONT-LIST':
      return []

    default:
      return state
  }
}
