/**
 * --------------------------------------------------------------------------
 * hamburger.js
 * Hamburger extension to Bootstrap Collapse
 * --------------------------------------------------------------------------
 */

import {
  defineJQueryPlugin,
  getSelectorFromElement
} from 'bootstrap/js/src/util'
import Data from 'bootstrap/js/src/dom/data'
import EventHandler from 'bootstrap/js/src/dom/event-handler'
import Manipulator from 'bootstrap/js/src/dom/manipulator'
import SelectorEngine from 'bootstrap/js/src/dom/selector-engine'
import Collapse from 'bootstrap/js/src/collapse'
import MobileToggles from './mobile-toggles'

/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */

const NAME = 'hamburger'
const DATA_KEY = 'uoe.hamburger'
const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'

const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`

const SELECTOR_DATA_TOGGLE = '[data-uoe-toggle="hamburger"]'
const SELECTOR_HAMBURGER = '.hamburger'
const CLASS_NAME_EXPANDED = 'hamburger-expanded'

const Default = {
  toggle: true,
  parent: ''
}

/**
 * ------------------------------------------------------------------------
 * Private methods
 * ------------------------------------------------------------------------
 */

function hamburgerIcon() {
  const icon = document.createElement('span')
  icon.className = 'hamburger'
  icon.setAttribute('aria-hidden', 'true')
  return icon
}

/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */

class Hamburger extends Collapse {
  // Public

  toggle() {
    if (!this._hamburger) {
      this._hamburger = this._getHamburger()
    }

    super.toggle()
  }

  show() {
    if (!this._hamburger.classList.contains(CLASS_NAME_EXPANDED)) {
      this._hamburger.classList.add(CLASS_NAME_EXPANDED)
    }

    super.show()
  }

  hide() {
    if (this._hamburger.classList.contains(CLASS_NAME_EXPANDED)) {
      this._hamburger.classList.remove(CLASS_NAME_EXPANDED)
    }

    super.hide()
  }

  // Private

  _getHamburger() {
    return SelectorEngine.findOne(SELECTOR_HAMBURGER, this._config.trigger)
  }

  // Static

  static hamburgerInterface(element, config) {
    let data = Data.getData(element, DATA_KEY)
    const _config = {
      ...Default,
      ...Manipulator.getDataAttributes(element),
      ...(typeof config === 'object' && config ? config : {})
    }

    if (!data && _config.toggle && typeof config === 'string' && /show|hide/.test(config)) {
      _config.toggle = false
    }

    if (!data) {
      data = new Hamburger(element, _config)
    }

    if (typeof config === 'string') {
      if (typeof data[config] === 'undefined') {
        throw new TypeError(`No method named "${config}"`)
      }

      data[config]()
    }
  }

  static jQueryInterface(config) {
    return this.each(function () {
      Hamburger.hamburgerInterface(this, config)
    })
  }
}

/**
 * Ensure hamburger icons are on all elements
 */
MobileToggles.addIcons(SELECTOR_DATA_TOGGLE, hamburgerIcon(), SELECTOR_HAMBURGER)

/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */

EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
  // preventDefault only for <a> elements (which change the URL) not inside the collapsible element
  if (event.target.tagName === 'A' || (event.delegateTarget && event.delegateTarget.tagName === 'A')) {
    event.preventDefault()
  }

  const triggerData = Manipulator.getDataAttributes(this)
  const selector = getSelectorFromElement(this)
  const selectorElements = SelectorEngine.find(selector)

  for (const element of selectorElements) {
    const data = Data.getData(element, DATA_KEY)
    let config
    if (data) {
      // update parent attribute
      if (data._parent === null && typeof triggerData.parent === 'string') {
        data._config.parent = triggerData.parent
        data._parent = data._getParent()
      }

      config = 'toggle'
    } else {
      config = triggerData
    }

    Hamburger.hamburgerInterface(element, config)
  }
})

/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
 * add .Hamburger to jQuery only if jQuery is present
 */

defineJQueryPlugin(NAME, Hamburger)

export default Hamburger
