// I'm sorry you have to see JavaScript on this site, but it's here to
// enable a manual dark mode toggle. When dark mode support improves I
// can probably remove this again.
function setMode(mode, val) {
  if (val) document.body.classList.add(mode)
  else document.body.classList.remove(mode)
  Array.from(document.querySelectorAll('a'))
  .filter( el => el.href.indexOf('cosmic.voyage') !== -1)
  .map( el => {
    var url = el.href
    var p = url.indexOf('?') !== -1 ? url.substr(url.indexOf('?')) : ''
    var baseURL = url.split('?')[0]
    const params = new URLSearchParams(p)
    if (val) {
      params.append(mode, 1)
    } else {
      params.delete(mode)
    }
    p = params.toString()
    el.href = baseURL + (p ? '?' + p : '')
  })
}

window.addEventListener('DOMContentLoaded', function() {
  var params = new URLSearchParams(window.location.search)
  if (params.has('dark')) {
    setMode('dark', true)
  } else if (params.has('light')) {
    setMode('light', true)
  }
})
