/**
 * --------------------------------------------------------------------------
 * icons.js
 * SVG icons helpers.
 *
 * DEPRECATED in favour of using Bootstrap Icons classes.
 * An example SVG Sprite is provided with a small set of commonly used icons
 * in dist/media/icons.svg
 * --------------------------------------------------------------------------
 */

import {
  onDOMContentLoaded
} from 'bootstrap/js/src/util/index.js'

const Icons = {
  loading: false,

  spriteLoader(spritePath = '../media/icons.svg') {
    // eslint-disable-next-line no-console
    console.warn('Icons.spriteLoader is deprecated; please use Bootstrap classes instead. ' +
      'Not all Bootstrap Icons are included in the provided SVG sprite')

    if (this.loading) {
      return
    }

    this.loading = true

    const _loadSprite = function () {
      const xhr = new XMLHttpRequest()
      xhr.open('GET', this.spritePath, true)
      xhr.responseType = 'document'
      xhr.addEventListener('load', () => {
        const el = xhr.responseXML.documentElement
        const { style } = el
        style.display = 'none'
        return document.body.insertBefore(el, document.body.childNodes[0])
      })

      return xhr.send()
    }.bind({ spritePath })

    onDOMContentLoaded(_loadSprite)
  }
}

export default Icons
