/**
 * --------------------------------------------------------------------------
 * helpers.js
 * Helper functions
 * --------------------------------------------------------------------------
 */

import { getUID } from 'bootstrap/js/src/util'

const ID_PREFIX = 'uoe-'
const timeouts = {}

/**
 * Public methods
 */

const uoeUID = function () {
  return getUID(ID_PREFIX)
}

const escapeHtml = function (text) {
  return text
    .replaceAll('&', '&amp;')
    .replaceAll('<', '&lt;')
    .replaceAll('>', '&gt;')
    .replaceAll('"', '&quot;')
    .replaceAll('\'', '&#039;')
}

const delay = s => new Promise(resolve => {
  setTimeout(resolve, s * 1000)
})

const debounce = (func, binding, timeout = 200) => {
  return (...args) => {
    if (timeouts[func.name]) {
      clearTimeout(timeouts[func.name])
    }

    timeouts[func.name] = setTimeout(() => {
      timeouts[func.name] = undefined
      func.apply(binding, args)
    }, timeout)
  }
}

const DAYS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

export {
  uoeUID,
  escapeHtml,
  delay,
  debounce,
  DAYS,
  MONTHS
}
