var anyLogs = <%= !!@logs.any? %>;
var working = <%= !!@working %>;

<% if @logs.any? %>
 var logHtml = '<%= escape_javascript(render(@logs)) %>';
<% else %>
 var logHtml = '';
<% end %>

if (anyLogs) {
  ConsoleUpdater.parsing = working;
  $("#console").append(logHtml);
  $("#console").prop({ scrollTop: $("#console").prop("scrollHeight") });
  setTimeout(ConsoleUpdater.updateConsole, 2000);

  var close_url = $('#result').data('close-url');

  if (!working && close_url) {
    if (window.Turbo) {
      $('#modal-console').on('hide', function() {
        Turbo.cache.clear();
        Turbo.visit(close_url);
      });
      $("#modal-console .modal-footer").removeClass('d-none');
    } else if (window.location != close_url)  {
      // if Turbo isn't present, do it the old-fashioned way:
      window.location = close_url;
    } else { // if .location == close_url, we need to do it this way:
      window.location.reload(true);
    };
  } else if (!working && !close_url) {
    // Throw custom job-done event
    var customEvent = new CustomEvent('job-done', {detail: {state: 'end'}});
    window.dispatchEvent(customEvent);
  }
} else {
  setTimeout(ConsoleUpdater.updateConsole, 4000);
}
