/* See license.txt for terms of usage */

"use strict";

const { Firebug } = require("../lib/index.js");
const { defer } = require("sdk/core/promise");

/**
 * xxxHonza: TODO description
 *
 * @param toolbox
 * @param config
 */
function waitForMessage(toolbox, config) {
  let deferred = defer();
  let chrome = Firebug.getChrome(toolbox);
  let consoleOverlay = chrome.getOverlay("webconsole");
  let frame = consoleOverlay.panel.hud.ui;
  let doc = consoleOverlay.getPanelDocument();
  let result = doc.querySelector(config.cssSelector);

  if (result) {
    deferred.resolve(result)
    return deferred.promise;
  }

  function onMessages(event, messages) {
    let doc = consoleOverlay.getPanelDocument();
    let result = doc.querySelectorAll(config.cssSelector);
    if (result.length) {
      frame.off("new-messages", onMessages);
      deferred.resolve(result);
    }
  }

  frame.on("new-messages", onMessages);

  return deferred.promise;
}

/**
 * xxxHonza: TODO description
 *
 * @param toolbox
 * @param sidePanelId
 * @returns
 */
function openSidePanel(toolbox, sidePanelId) {
  let deferred = defer();
  let chrome = Firebug.getChrome(toolbox);
  let panel = toolbox.getPanel("webconsole")
  let consoleOverlay = panel._firebugPanelOverlay;
  let jsterm = consoleOverlay.getTerminal();

  jsterm.once("sidebar-created", (eventName, sidebar) => {
    if (!sidebar) {
      reject(new Error("can't get the sidebar"));
    }

    sidebar.once(sidePanelId + "-ready", () => {
      sidebar.select(sidePanelId);
      deferred.resolve({
        panel: panel,
        sidePanel: sidebar.getTab(sidePanelId)
      });
    });
  });

  consoleOverlay.toggleSidebar();

  return deferred.promise;
}

// Exports from this module
exports.waitForMessage = waitForMessage;
exports.openSidePanel = openSidePanel;

@@ -165,75 +165,417 @@
 %0A * 
-xxxHonza: TODO description%0D%0A *%0D%0A * @param toolbox%0D%0A * @param config
+Wait for a message displayed in the Console panel.%0D%0A *%0D%0A * @param %7BToolbox%7D toolbox The current toolbox instance.%0D%0A * @param %7BObject%7D config Configuration object with the following flags:%0D%0A * %7BString%7D cssSelector CSS selector that identifies the message%0D%0A * (or messages) to wait for.%0D%0A *%0D%0A * @returns %7BPromise%7D A promise that is resolved when a message(s)%0D%0A * that matches provided CSS selector is displayed.
 %0D%0A *
@@ -1341,57 +1341,402 @@
 %0A * 
-xxxHonza: TODO description%0D%0A *%0D%0A * @param toolbox
+Open side panel in the Console panel. The API expects that the%0D%0A * side panel is implemented using %7B@ToolSidebar%7D widget from%0D%0A * the platform.%0D%0A *%0D%0A * xxxHonza: this API should be used for any panel in the toolbox, but%0D%0A * not every panel is using %7B@ToolSidebar%7D, see also:%0D%0A * https://bugzilla.mozilla.org/show_bug.cgi?id=1074710%0D%0A *%0D%0A * @param %7BToolbox%7D toolbox The current toolbox instance.
 %0D%0A *
@@ -1742,16 +1742,25 @@
 * @param
+ %7BString%7D
  sidePan
@@ -1763,29 +1763,251 @@
 ePanelId
-%0D%0A * @returns
+ ID of the side panel to be selected%0D%0A * by default.%0D%0A *%0D%0A * @returns %7BPromise%7D A promise that is resolved when the side panel%0D%0A * is opened and default panel selected and loaded (i.e. panel content%0D%0A * iframe loaded and ready to use).
 %0D%0A */%0D%0Af

Update JS doc