import { defaultIcons } from './consts'
import { IconDefinition } from './types'

/**
 * Holds the collection of selected predefined icons. Can be extended with more icons by the consumer.
 * When using icons, they will be checked in library before loading them from CDN.
 */
class IconsCache {
  cache: Map<string, IconDefinition> = new Map()

  add = (...icons: IconDefinition[]) => {
    icons.forEach(icon => {
      this.cache.set(icon.name, icon)
    })
  }

  get = (name: string) => {
    const icon = this.cache.get(name)

    if (!icon) {
      /* eslint-disable-next-line no-console */
      console.error(`Icon ${name} does not exist in the library.`)
    }

    return icon
  }

  has = (name: string) => {
    return this.cache.has(name)
  }

  init = () => {
    this.add(...Object.values(defaultIcons))
  }

  reset = () => {
    this.cache.clear()
  }
}

const cache = new IconsCache()

cache.init()

export default cache
