/**
 * Crea los elementos option del array que se pase como parametro
 * @method createSelectOptions
 * @param {JSONArray} obj array de objetos
 * @param {String} valueName la key del json que se usará como value del elemento option
 * @param {String} textName la key del json que se usará como text del elemento option
 * @returns {Promise} Promise retorna status, msg
 */
let createSelectOptions = (obj, valueName, textName) => {
  let options = ''
  obj.forEach((data) => {
    options += `<option value="${data[valueName]}">${data[textName]}</option>`
  })
  return options
}

let customSwal = {
  alert(title, text, type) {
    let colors = {
      success: '#66BB6A',
      error: '#EF5350',
      warning: '#FF7043'
    }
    let btnColor = colors[type]
    return new Promise((resolve, reject) => {
      swal({
        title: title,
        text: text,
        confirmButtonColor: btnColor,
        type: type
      }, (isConfirm) => {
        resolve()
      })
    })
  }
}

/**
 * Get the closest matching element up the DOM tree.
 * @private
 * @param  {Element} elem     Starting element
 * @param  {String}  selector Selector to match against
 * @return {Boolean|Element}  Returns null if not match found
 */
let getClosest = (elem, selector) => {

  // Element.matches() polyfill
  if (!Element.prototype.matches) {
    Element.prototype.matches =
      Element.prototype.matchesSelector ||
      Element.prototype.mozMatchesSelector ||
      Element.prototype.msMatchesSelector ||
      Element.prototype.oMatchesSelector ||
      Element.prototype.webkitMatchesSelector ||
      function (s) {
        var matches = (this.document || this.ownerDocument).querySelectorAll(s),
          i = matches.length
        while (--i >= 0 && matches.item(i) !== this) {
        }
        return i > -1
      }
  }
  // Get closest match
  for (; elem && elem !== document; elem = elem.parentNode) {
    if (elem.matches(selector))
      return elem
  }
  return null
}
