export const env = require('./env.json')
export const msgBox = document.getElementById(env.querySelectors.console.slice(1))

/**
 *
 * @type {{}}
 */
const monthsNamesFull = env.lists.monthsFull

export const randomID6n = () => Math.floor(100000 + Math.random() * 900000)

export const parseURL = url => new URL(url)



/**
 *
 * @param string
 * @returns {string}
 */
export const ucWords = string => {
  return string.split('_').map(word => word.charAt(0).toUpperCase() + word.substr(1).toLowerCase()).join(' ')
}


/**
 *
 * @param date
 * @returns {string}
 */
export const formatDate = date => {
  const dt = new Date(date)
  return [monthsNamesFull[dt.getMonth()], dt.getDate(), `, ${dt.getFullYear()}`].join(' ')
}

/**
 *
 * @param str
 * @returns {*[]}
 */
export const findDates = str => {
  const dates = []
  const regex = new RegExp('[0-3]?[0-9].[0-3]?[0-9].(?:[0-9]{2})?[0-9]{2}', 'mg')

  for (const match of (str || '').matchAll(regex)) {
    dates.push(match[0])
  }

  return dates
}

/**
 *
 * @param args
 * @returns {*}
 */
export const createNode = (...args) => {
  const [tag, attrs, content, callback] = args
  const node = document.createElement(tag)

  for (const [key, value] of Object.entries(attrs || [])) {
    node.setAttribute(key, value)
  }

  node.innerHTML = content || ''

  return node
}

/**
 *
 * @param node
 */
export const destroyNode = (node) => {

  if (!(node instanceof Node) && typeof node === 'string') {
    node = document.querySelector(node)
  }

  try {
    node.remove()
  } catch (e) {}

}


