/**
 * public/src/js/logger.js - delish
 * 
 * Licensed under MIT license.
 * Copyright (C) 2017 Karim Alibhai.
 */
/* globals trackJs */

const util = require('util')
    , debounce = require('debounce')

let currentLog = document.querySelector('.lead')
  , nextLog = document.querySelector('.lead.next')

/**
 * Updates the current log status.
 * @param {?Function} done a callback to execute if the log happens
 * @param {String} message the string with formatting
 * @param {...Object} values any values to plug in
 */
export const log = debounce(function (done) {
  let text

  if (typeof done === 'function') {
    text = util.format.apply(util, [].slice.call(arguments, 1))
  } else {
    text = util.format.apply(util, arguments)
    done = () => 0
  }

  // transition to the new log
  nextLog.innerText = text
  currentLog.classList.add('hide')
  nextLog.classList.remove('next')

  // after the CSS transition completes, swap
  // the log elements out
  setTimeout(() => {
    let parent = currentLog.parentElement

    parent.removeChild(currentLog)
    currentLog = nextLog

    nextLog = document.createElement('p')
    nextLog.classList.add('lead')
    nextLog.classList.add('next')
    parent.appendChild(nextLog)
  }, 700)

  // return reference to the log element used
  done(nextLog)
}, 700)

/**
 * Handle task failure.
 */
export function fail(err) {
  trackJs.track(err)

  document.documentElement.classList.add('error')

  log(logElm => {
    logElm.classList.add('error-msg')
    logElm.classList.add('col-8')
    logElm.classList.add('offset-2')
  }, 'Things have gone horribly wrong. I think your computer is going to explode. But what do I know, I\'m just an error message.')
}
@@ -303,238 +303,67 @@
 t')%0A
-%0A/**%0A * Updates the current log status.%0A * @param %7B?Function%7D done a callback to execute if the log happens%0A * @param %7BString%7D message the string with formatting%0A * @param %7B...Object%7D values any values to plug in%0A */%0Aexport 
+  , appHasFailed = false%0A%0A/**%0A * Actual logger.%0A */%0A
 const 
+_
 log 
@@ -1134,16 +1134,334 @@
 , 700)%0A%0A
+/**%0A * Updates the current log status.%0A * @param %7B?Function%7D done a callback to execute if the log happens%0A * @param %7BString%7D message the string with formatting%0A * @param %7B...Object%7D values any values to plug in%0A */%0Aexport const log = function () %7B%0A  if (!appHasFailed) %7B%0A    return _log.apply(this, arguments)%0A  %7D%0A%7D%0A%0A
 /**%0A * H
@@ -1480,16 +1480,16 @@
 ailure.%0A
-
  */%0Aexpo
@@ -1512,16 +1512,38 @@
 (err) %7B%0A
+  appHasFailed = true%0A
   trackJ
@@ -1607,18 +1607,19 @@
 rror')%0A%0A
-
   
+_
 log(logE

Fix logger races again