/**
 * The message handler IDs.
 * @type {string}
 */
// LINT.IfChange
const SUPPORT_URL_NAME = 'supportURL';
const ON_ERROR = 'onError';
// LINT.ThenChange(../GHKSearchResultWebViewController.m:message_handler_names)

/**
 * The message keys.
 * @type {string}
 */
// LINT.IfChange
const SUPPORT_URL = 'supportURL';
const JS_ERROR_MESSAGE = 'jsErrorMessage';
const JS_ERROR_URL = 'jsErrorURL';
// LINT.ThenChange(../GHKSearchResultWebViewController.m:message_keys)

/**
 * Defines the callback function for the Search Results object, which posts a
 * message to be read by HelpKit's search result WebView.
 * @param {!Page} page The page that contains the Search Results object.
 */
window.search_results = function(page) {
  const searchResultsObj = page.getChild();
  if (searchResultsObj == undefined) {
    return;
  }
  searchResultsObj.setSupportUrlCallback(function(supportUrl) {
    window.webkit.messageHandlers[SUPPORT_URL_NAME].postMessage({
      [SUPPORT_URL]: supportUrl,
    });
  });
};

/**
 * Defines the js error callback function, which is invoked when an error occurs
 * during js loading.
 * @param {string} errorMsg The js error message.
 * @param {string} url The URL that js error occurs when loading.
 * @param {!integer} lineNumber The line number that js error occurs.
 */
window.onerror = function(errorMsg, url, lineNumber) {
  window.webkit.messageHandlers[ON_ERROR].postMessage({
    [JS_ERROR_MESSAGE]: errorMsg,
    [JS_ERROR_URL]: url,
  });
};
